Check if Given Program is Running and Start it if not
Written by Guillermo Garron
Date: 2012-04-25 11:13:45 00:00
I'm running a program that from time to time stop working (Network issues).
I'm now using this bash shell script to re-start it if it is not already running.
vim ~/restart-if-not-running.sh
Here are the contents:
#!/bin/bash
# Use ps and grep to check if the program is running
ps -ef | grep -v grep | grep offlineimap # grep -v grep, keeps grep command itself out of the question
# if it is not running, start it.
if [ $? -eq 1 ]
then
# Your code to start the program goes here
# Could be something like:
nohup rsync -av /some/folder /some/other/folder &
else
# What to do if it is already or still running
echo "The program is still running."
fi
I'm using nohup to detach the other program from this script.