Monday, May 19, 2014

killing processes

On a server I was working on, a bunch of stuck processes had accumulated. They were all started by cron, and consisted of a shell command that launched a python process, and was waiting on it. (It was the python processes that were stuck).  If I could kill the python process, the shell would terminate on its own. The killall command did not work, as I could not figure out how to indicate that I wanted only particular python processes, not all of them. I eventually worked up a pipeline of commands to kill the processes:

for i in `ps waux | grep common_string | grep -v shell_string | tr -s ' ' | cut -s -f 2 -d ' '` ; do kill $i ; done

The ps command lists all processes. The grep for common_string filters for both the shell and python processes. I could not grep for just the python processes because their entire command line is contained within the shell command line. Thus I needed to filter out the shell processes with the grep -v shell-string. The ps command leaves a lot of spaces in its output, so I squeeze that down to single spaces with the tr -s command. And finally I cut out the second space-separated field to yield the list of process IDs I want to kill. I then run those through a for loop to kill each one in turn.

Yes, developing this command probably took longer than typing all the kill commands manually, but by blogging my results hopefully I can leverage this technique in the future.

0 Comments:

Post a Comment

<< Home