Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been messing around with various ways to do this in bash, and I've found pgrep program > /dev/null || program to be the shortest/most elegant way of doing this. Is there a better method than this?

share|improve this question
up vote 8 down vote accepted

With pgrep, you might match other programs than just yours. If you want to be sure to match the right program, use pidof instead.

You could also use start-stop-daemon with a PID file. start-stop-daemon will check if the program is running and start it if required. This is what is used in most init scripts.

share|improve this answer
Going through the man file of start-stop-daemon and it looks nice. Thanks for the insight! – aperson Aug 26 '10 at 8:26
You might also be interested in the /lib/lsb/init-functions library, which most init scripts parse to use wrapper functions such as start_daemon or pidofproc. – ℝaphink Aug 26 '10 at 8:41
[ -z `pidof program` ] || echo "program runing"
# or 
[ -z `pidof program` ] && OtherProgram 
share|improve this answer
Could you please explain to the OP what to do with this command? – hexafraction Aug 16 '12 at 20:14

Your Answer


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.