2

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:

Sam
  • 7,252
  • 16
  • 46
  • 65
linuxnoob
  • 675
  • 2
  • 6
  • 17
  • 1
    Instead of `grep -v grep` you could just grep for `grep [C]Ccam` to begin with. This expression won't match itself. – FatalError Sep 13 '13 at 15:50
  • better yet, `pgrep CCcam` – micke Sep 13 '13 at 15:52
  • OK, I will try both of them.. From another test script I get.. ERROR: CCcam still runs with pid: Would that be a hint as to where this problem lies? – linuxnoob Sep 13 '13 at 15:55
  • it is so much easier to debug what your code is doing when you turn on the shell debugging feature. `set -x` will show you each line with values for variables substituted in place. `set -vx` is a little more verbose, but can be confusing (in a different way than just `set -x` ;-) . I think if you use the debug, you'll see that something is being set into $process that shouldn't be there. Also, awk can match patterns, so take out all greps and use `| awk '/[C]Ccam/{print $1}'`. Good luck. – shellter Sep 13 '13 at 16:22
  • Im sorry guys but I did not know how to implement the answers you gave me into my script, my linux is very poor at the moment.. – linuxnoob Sep 13 '13 at 21:16
  • Is your script perhaps called `check-CCcam.sh` or something that contains the name you are `grep`ping for? There's a reason `pgrep` and/or `pidof` exist - the `ps ... | grep ... | awk ...` construct is fragile in that way... – twalberg Jun 25 '14 at 20:38

2 Answers2

4
if pidof -s CCCam > /dev/null; then
    echo 'It is already running!'
else
    echo 'process not found...'
fi
Aleks-Daniel Jakimenko-A.
  • 10,335
  • 3
  • 41
  • 39
4

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
iamauser
  • 11,119
  • 5
  • 34
  • 52