Is there any reason why this script always returns “running”, regardless of whether my process is started or stopped?
if ps ax | grep -v grep | grep "processName" > /dev/null
then
echo $"running"
else
echo $"not running"
fi
Thank you very much
UPDATE :
I add a full example of my script, maybe there is something wrong elsewhere.
case "$1" in
start)
# Start daemons.
echo -n $"Starting daemon: "
;;
stop)
# Stop daemons.
echo -n $"Shutting down: "
echo
;;
status)
pgrep -f "ProcessName" > /dev/null
if [ $? -eq 0 ]; then
echo $"ProcessName is running"
else
echo $"ProcessName is not running"
fi
;;
restart)
$0 stop
$0 start
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac
UPDATE 2 :
[user@dev init.d]# pgrep -f "MyProcess" > /dev/null
[user@dev init.d]# echo $?
0
[user@dev init.d]# service MyProcess stop
Shutting down MyProcess: Terminated
[user@dev init.d]# pgrep -f "MyProcess" > /dev/null
[user@dev init.d]# echo $?
1
But if [ $? -eq 0 ]; then seems to be TRUE all the time
Try this instead:
The brackets around the first letter of processName means do don’t need the “grep -v grep”, while the -q means we don’t need the pipe to /dev/null
$? gives you the return code of the previous command executed. Hence, testing if it were 0 would indicate if “grep” found what it was looking for.
Update
If your process name is really short (say “cup”), you might get a false positive as it may match other processes too (say “cupsd”). You can overcome this by having grep match whole words – add the “-w” flag.
Not that this technique is not perfect. You may end up matching entries in the username/date fields. If that happens, look up “man ps” and be more selective of what you print out before doing a grep. Alternatively, prefilter the output with awk to extract only the column showing process/cmd name . E.g:
Update 2
you can also use pgrep as suggested in answer below.
For really short process names, you might want to specify word boundaries (\b) before and after your process name to prevent overmatching (as described above)
Update 3
From the updated question, I see that you’re running it from an init script. There’s always a danger of pgrep matching the script itself. Try:
That excludes the PID of the script from pgrep matches.
Update 4
(final update? fingers crossed)
If the init script is run via the “service” command, then we need to filter out the parent PID as well. How about: