views:

131

answers:

2

Hello,

I am trying to check if a process is running. If it is running I want a return value of 'OK' and if not a return value of 'Not OK'. I can only use 'ps' without any other arguments attached (eg. ps -ef) if thats the correct term. The code I have is:

if ps | grep file; then  echo 'OK'; else  echo 'NO'; fi

The problem with this is that it does not search for the exact process and always returns 'OK', I don't want all the information to appear I just want to know if the file exists or not.

Thanks for any help.

+6  A: 

Your code always returns 'OK', because grep finds itself in the process list ('grep file' contains the word 'file'). A fix would be to do a `grep -e [f]ile' (-e for regular expression), which doesn't find itself.

tangens
+2  A: 
ps | grep -q '[f]ile'
Ignacio Vazquez-Abrams