How to monitor or kill a process which has been started by cron

killprocessscheduling

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?

Best Answer

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:

Related Question