Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
Questions - which system, which scheduler? cron? –  Danny Staple Jun 7 '11 at 16:20
    
@Danny yeah Cron. –  Harish Kurup Jun 7 '11 at 16:24
1  
Down 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? –  Bruce Ediger Jun 7 '11 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 Kurup Jun 8 '11 at 5:20

3 Answers 3

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:

share|improve this answer

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.

share|improve this answer

I'd suggest popping their PID ($$) into a file after you spawn them. You can then use this to kill the process.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.