Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
share|improve this question
add comment

3 Answers 3

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
share|improve this answer
add comment

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.

share|improve this answer
    
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 '13 at 19:04
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.