Sunday 25 October 2009

Silently killing multiple Linux processes

In the last few months I've been working with a lot of concurrent and parallel programs. One of the problems with this sort of programming is when you get it wrong, you can end up with a whole bunch of child processes running detached from their parent, like this:

24827 pts/3 00:00:00 python  24828 pts/3 00:00:00 python  24829 pts/3 00:00:00 python  24830 pts/3 00:00:00 python  24831 pts/3 00:00:00 python  24832 pts/3 00:00:00 python  24833 pts/3 00:00:00 python  24834 pts/3 00:00:00 python  24835 pts/3 00:00:00 python  24836 pts/3 00:00:00 python  
Right now I have 103 of these orphaned Python processes. Using killall doesn't seem to work, so what we need to do is pass their PIDs directly to 'kill -9' . ps will print a whole bunch of nice information, so we need to coerce ps into just printing out the information we want, like this:

$ ps h -C python -o pid  ...  24814  24819  24820  24821  24822  ...  

Then we can pass that to kill -9, like this:

$ kill -9 `ps h -C python -o pid`  

Now we can find out how many Python processes are still running:

$ ps h -C python -o pid | wc -l  0  

zero -- just what we want :-)

Posted via web from snim2's posterous

No comments:

Post a Comment