Ubuntu – How to determine status of upstart job in bash script

bashupstart

How do you determine if an upstart job is running inside of a Bash script? That is I need a "boolean" value to do something like:

#!/bin/bash
if [ determine_if_job_x_is_running ]; then
  echo "I see upstart job X is running, please stop it before ..."
fi

Best Answer

Create your own Bash function and put this in your ~/.bashrc:

check_upstart_service(){
    status $1 | grep -q "^$1 start" > /dev/null
    return $?
}

I really dislike the way of parsing output, but I don't see another obvious way. And in this case the output of <service name> start is very reliable as it's specified in the Upstart documentation.

Now you can use it like this:

if check_upstart_service ssh; then echo "running"; else echo "stopped"; fi