lbussy
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Them pesk backticks again! Works now, thanks for the help.
should say:
RES=`ps -ef | grep 'bash' | grep -v 'grep'`
RES=`ps -ef | grep 'bash' | grep -v 'grep'`
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 :-)
RES=`ps -ef | grep 'bash' | grep -v 'grep'
if [ -z "$RES" ];then
...
fi
which makes it easier to debug :-)