In UNIX, I have a utility, say 'Test_Ex', a binary file. How can I write a job or a shell script(as a cron job) running always in the background which keeps checking if 'Test_Ex' is still running every 5 seconds(and probably hide this job). If it is running, do nothing. If not, delete a directory at the specified path.
+2
A:
Try this script:
pgrep Test_Ex > /dev/null || rm -r dir
If you don't have pgrep
, use
ps -e -ocomm | grep Test_Ex || ...
instead.
Aaron Digulla
2010-06-30 12:34:40
thanks.. the procedure is to edit crontab and add this line. Can this manual task be done using a shell script as part of an installation process?
Abhi
2010-06-30 13:34:28
Dennis Williamson
2010-06-30 15:18:33
+2
A:
The best way to do this is to not do it. If you want to know if Test_Ex is still running, then start it from a script that looks something like:
#!/bin/sh Test_Ex logger "Test_Ex died" rm /p/a/t/h
or
#!/bin/sh while ! Test_ex do logger "Test_Ex terminated unsuccesfully, restarting in 5 seconds" sleep 5 done
Querying ps regularly is a bad idea, and trying to monitor it from cron is a horrible, horrible idea. There seems to be some comfort in the idea that crond will always be running, but you can no more rely on that than you can rely on the wrapper script staying alive; either one can be killed at any time. Waking up every 10 seconds to query ps is just a waste of resources.
William Pursell
2010-07-05 05:19:55