Most elegant way to check if a process is running, and start if not?
Solution 1:
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.
Solution 2:
[ -z `pidof program` ] || echo "program runing"
# or
[ -z `pidof program` ] && OtherProgram