I have some bots, which are run by the System scheduler at given time interval. But sometimes due to some logical error I have to stop these bots manually. How can I find these processes run by the scheduler and kill them?
-
Questions - which system, which scheduler? cron?– Danny StapleJun 7, 2011 at 16:20
-
@Danny yeah Cron.– Harish KurupJun 7, 2011 at 16:24
-
1Down that path lies Madness. Don't work around bugs by scheduling process killers, fix the bugs. What happens when the process killer has a bug, doesn't kill the desired process, and hangs itself? WIll you write a killer killer?– user732Jun 7, 2011 at 18:10
-
@Bruce thats right, if the bug is a runtime or a compile time error the process will kill itself...but what about the logical error which will keep running in a infinite loop...that what i want to break..– Harish KurupJun 8, 2011 at 5:20
3 Answers
You can kill processes by name. For example, on Linux, *BSD and Solaris, pkill myprogram
kills all the processes whose name contains myprogram
(use pkill '^myprogram$'
for an exact match). If you run it as a non-root user, only that user's processes will be killed, and there are further options to control matching (see the manual on your system for details).
If you want to specifically target processes started by the scheduler, and you're killing the processes manually, you can run ps f
(Linux only) or pstree
(Linux only) or ptree
to display the processes in a tree, and see which processes were started by cron.
If you want to be able to kill these processes automatically in a homemade method, make them store their process ID in a file. This kind of file is called a pidfile when it's used to only have a single instance of the process running (which may or may not be something you want). If you want multiple instances, store the PIDs in separate files in a common directory; here's a shell snippet that does this:
pid_dir=/var/run/myprogram # must have been created e.g. at boot time
myprogram &
pid_file=$pid_dir/$!.pid
touch "$pid_file"
wait
rm "$pid_file"
A better solution, if you have hard criteria to detect runaway processes, is to use a general monitoring program, or in simple cases just put a limit on how long the process is allowed to run. You may find these links helpful:
- “Monit can start a process if it does not run, restart a process if it does not respond and stop a process if it uses too much resources.”
- I need help with a cronjob to watch for runaway processes and kill them
- How to limit resource usage to save CPU+RAM for a certain process?
- Is there a way to limit the amount of memory a particular process can use in Unix?
I'd suggest popping their PID ($$) into a file after you spawn them. You can then use this to kill the process.
A basic try/catch branch in most programming languages to find infinite loops or other cases of errors should work. Possibly an inactive/unresponsive timer on the processes to watch for hangs.