11

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

5 Answers 5

9

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
1
  • 2
    You might want to amend this to status $1 2> /dev/null | grep -q "^$1 start" > /dev/null 2> /dev/null just to make sure it's silent.
    – David G
    Aug 4, 2015 at 20:23
1

Normally you use a PID file but you can also use pgrep to check your processes. Assume your service is called jobX this will work:

if [ $(pgrep jobX) ]; then

Or even better

if pgrep jobX > /dev/null 2>&1
1

Based on String contains in bash:

job='your_job_name'
job_status=$(status ${job})
if [[ ${job_status} == *running* ]]
  then
    # do whatever you need
  else
    # do whatever you need
fi

My first impulse was to use variation of code ImaginaryRobots provided

job='your_job_name'
dbus-send --system --print-reply --dest=com.ubuntu.Upstart \
  /com/ubuntu/Upstart/jobs/${job}/_ \
  org.freedesktop.DBus.Properties.Get string:'' string:state

which would return something like

method return sender=:1.0 -> dest=:1.94 reply_serial=2 variant string "running"

and use the solution above to check if the returned string contains "running". However case that job is not running dbus call will exit with status 1 instead returning "waiting" as I was expecting.

status ${job} would never exit with status 1 except in the case when there is no such job.

1
  • 1
    A variation ... isRunning=$(status ${job} | grep running | wc -l) will return 1 if it is running and 0 if it is not.
    – Naraen
    Apr 26, 2017 at 15:28
0

You would use DBUS to query the status of that particular service.

$ job=myjob
$ dbus-send --system --print-reply --dest=com.ubuntu.Upstart /com/ubuntu/Upstart/jobs/${job}/_ org.freedesktop.DBus.Properties.GetAll string:''

http://upstart.ubuntu.com/cookbook/#get-status-of-job-via-d-bus

Note that if you're writing your own upstart job, you should use upstart events or package dependencies instead.

1
  • Seems like this would just print some sort of text status that you would then have to parse to get a boolean value, right?
    – laslowh
    Feb 4, 2013 at 19:04
0

It seems that the upstart status command conforms to the init script specification from the Linux Standard Base project, meaning that you can assume an exit code of 0 means the program is running, an exit code of 1-3 means it is running, and any other exit code means an undefined status.

See: http://refspecs.linuxbase.org/LSB_3.0.0/LSB-PDA/LSB-PDA/iniscrptact.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .