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.

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

2 Answers 2

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
1  
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

 
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.