Link to home
Start Free TrialLog in
Avatar of lbussy
lbussyFlag for United States of America

asked on

Testing in a shell script (maybe some sed issues)

I'm trying to determine if a process is running and I'm having some issues with my script.  I'm using the '-z' flag on a test which should return True if the string is not a 0 value.  My thinking is that the  ps-ef command string (example below) through grep will only return a line if that process is running (that part works).  What does not work is the -z test always shows the process as running even if it's not.  I thought maybe there was a newline character in there so that's where I started messing with sed (shown below).
 
if [ -z 'ps -ef | grep "{any process name}" | grep -v "grep" | sed -e "/^ *$/d"' ];then
   echo 'Process not running\n'
else
   echo 'Process running\n'
fi

Unfortunately there's no flags on the executable that I am actually testing that will return a code if it's running (runs as a daemon) so I thought this was the best way.  I'm open to suggestions though.  Eventually this will run every 5 minutes and restart the process if it stops.

Thanks in advance for any help.
ASKER CERTIFIED SOLUTION
Avatar of tfewster
tfewster
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The backticks is one thing, the other are the quotes ("...") around the whole string as Tim already showed in his if statement. You can always create a variable

RES=`ps -ef | grep 'bash' | grep -v 'grep'
if [ -z "$RES" ];then
...
fi

which makes it easier to debug :-)
Avatar of lbussy

ASKER

Them pesk backticks again!  Works now, thanks for the help.
should say:
RES=`ps -ef | grep 'bash' | grep -v 'grep'`
Avatar of lbussy

ASKER

That works as well griessh, thanks for the alternative.  I had already accepted tfewster's answer before I read yours though.
No problem, he was first :-)