Script to check for process & restart program if not found

17,789

Solution 1

if pidof -s CCCam > /dev/null; then
    echo 'It is already running!'
else
    echo 'process not found...'
fi

Solution 2

You can also use pgrep in this case. It looks for the process name CCCam in proc table.

if [[ $(pgrep CCCam) ]]; then
   echo "CCCam is running";
else
   echo "Not Running, so I must do something";
   # DO something ....
fi
Share:
17,789
linuxnoob
Author by

linuxnoob

Updated on June 05, 2022

Comments

  • linuxnoob
    linuxnoob almost 2 years

    I am using this check script to see if a package called CCcam is running & restart it if it is not..

    #!/bin/sh 
    process=`ps auxwww | grep CCcam | grep -v grep | awk '{print $1}'`
    if [ -z "$process" ]; then
    echo "Couldn't find CCcam running. Restarting server-binary" >> /var/cccamlog/cccam.check 
    echo && date >>/var/cccamlog/cccam.check
    /usr/local/bin/CCcam -d >> /var/cccamlog/CCcam.log & 
    else echo "CCcam is still OK!" >> /var/cccamlog/cccam.check 
    fi 
    

    The script is reporting "CCcam is still OK!"

    But it is not running, If i search for the process using this command: ps x |grep -v grep |grep -c CCcam, I get 0 so I know the process is not running.

    Is there any other factors that I should take into account that might be fooling the check script into thinking CCcam is running? For example, could there be some kind of tag left after the program crashes/stops that the script is picking up on?

    From another test I get.. ERROR: CCcam still runs with pid:

  • GeneCode
    GeneCode over 4 years
    This answer is so sweet I now have diabetes.