I want to kill a whole process tree. What is the best way to do this using any common scripting languages? I am looking for a simple solution.
Join Stack Overflow to learn, share knowledge, and build your career.
|
You don't say if the tree you want to kill is a single process group. (This is often the case if the tree is the result of forking from a server start or a shell command line.) You can discover process groups using GNU ps as follows:
If it is a process group you want to kill, just use the |
|||||||||||||||||||||
|
|
I know that is old, but that is the better solution that i found:
|
|||
|
|
|
Old question, I know, but all the responses seem to keep calling ps, which I didn't like. This awk-based solution doesn't require recursion and only calls ps once.
Or on a single-line:
Basically the idea is that we build up an array (a) of parent:child entries, then loop around the array finding children for our matching parents, adding them to our parents list (p) as we go. If you don't want to kill the top-level process, then doing
just before the system() line would remove it from the kill set. Bear in mind that there is a race condition here, but that's true (as far as I can see) of all of the solutions. It does what I needed because the script I needed it for doesn't create lots of short-lived children. An exercise for the reader would be to make it a 2-pass loop: after the first pass, send SIGSTOP to all processes in the p list, then loop to run ps again and after the second pass send SIGTERM, then SIGCONT. If you don't care about nice endings then second-pass could just be SIGKILL, I suppose. |
||||
|
|
|
I develop the solution of zhigang, xyuri and solidsneck further:
This version will avoid killing its ancestry - which causes a flood of child processes in the previous solutions. Processes are properly stopped before the child list is determined, so that no new children are created or disappear. After being killed, the stopped jobs have to be continued to disappear from the system. |
|||
|
|
This will kill all processes that have the parent process ID 27888. Or more robust:
which schedule killing 33 second later and politely ask processes to terminate. See this answer for terminating all descendants. |
|||||||||||||||||||||
|
|
Kill all the processes belonging to the same process tree using the Process Group ID (
You can retrieve the
Special thanks to tanager and Speakus for contributions on Explanation
Further command lines
Limitation
Long story
Run the process tree in background using '&'
The command
The command
ConclusionI notice in this example I think |
|||||||||||||||||||||
|
|
brad's answer is what I'd recommend too, except that you can do away with
|
|||||
|
|
rkill command from pslist package sends given signal (or
|
|||
|
|
|
This script also work:
|
|||
|
|
|
Killing child process in shell script: Many time we need to kill child process which are hanged or block for some reason. eg. FTP connection issue. There are two approaches, 1) To create separate new parent for each child which will monitor and kill child process once timeout reached. Create test.sh as follows,
and watch processes which are having name as 'test' in other terminal using following command.
Above script will create 4 new child processes and their parents. Each child process will run for 10sec. But once timeout of 5sec reach, thier respective parent processes will kill those childs. So child won't be able to complete execution(10sec). Play around those timings(switch 10 and 5) to see another behaviour. In that case child will finish execution in 5sec before it reaches timeout of 10sec. 2) Let the current parent monitor and kill child process once timeout reached. This won't create separate parent to monitor each child. Also you can manage all child processes properly within same parent. Create test.sh as follows,
and watch processes which are having name as 'test' in other terminal using following command.
Above script will create 4 new child processes. We are storing pids of all child process and looping over them to check if they are finished their execution or still running. Child process will execution till CMD_TIME time. But if CNT_TIME_OUT timeout reach , All children will get killed by parent process. You can switch timing and play around with script to see behavior. One drawback of this approach is , it is using group id for killing all child tree. But parent process itself belong to same group so it will also get killed. You may need to assign other group id to parent process if you don’t want parent to be killed. More details can be found here, |
||||
|
|
|
The following has been tested on FreeBSD, Linux and MacOS X and only depends on pgrep and kill (the ps -o versions don't work under BSD). First argument is parent pid of which children have to be terminated. second argument is a boolean to determine whether the parent pid has to be terminated too.
This will send SIGTERM to any child / grandchild process within a shell script and if SIGTERM doesn't succeed, it will wait 10 seconds and then send kill. Earlier answer: The following also works but will kill the shell itself on BSD.
|
||||
|
|
|
type |
|||||
|
|
In sh the jobs command will list the background processes. In some cases it might be better to kill the newest process first, e.g. the older one created a shared socket. In those cases sort the PIDs in reverse order. Sometimes you want to wait moment for the jobs to write something on disk or stuff like that before they stop. And don't kill if you don't have to!
|
|||
|
|
|
If you want to kill a process by name:
or
|
|||
|
|
|
Here is a variation of @zhigang's answer which does without AWK, relying only on Bash's native parsing possibilities:
It seems to work fine on both Macs and Linux. In situations where you can't rely on being able to manage process groups -- like when writing scripts for testing a piece of software which must be built in multiple environments -- this tree-walking technique is definitely helpful. |
||||
|
|
|
To kill a process tree recursively, use killtree():
|
|||||||||||||||||||||
|
|
Based on zhigang's answer, this avoids self-killing:
|
|||||
|
|
I can't comment (not enough reputation), so I am forced to add a new answer, even though this is not really an answer. There is a slight problem with the otherwise very nice and thorough answer given by @olibre on Feb 28. The output of
|
|||||
|
|
It's super easy to do this with python using psutil. Just install psutil with pip and then you have a full suite of process manipulation tools:
|
|||
|
|
|
The following shell function is similar to many of the other answers, but it works both on Linux and BSD (OS X, etc) without external dependencies like
|
|||||||||||||||||||||
|
|
Modified version of zhigang's answer:
|
|||||||||
|
|
Inspired by ysth’s comment
|
|||||
|
|
|||||||||
|
|
It is probably better to kill the parent before the children; otherwise the parent may likely spawn new children again before he is killed himself. These will survive the killing. My version of ps is different from that above; maybe too old, therefore the strange grepping... To use a shell script instead of a shell function has many advantages... However, it is basically zhigangs idea
|
|||||
|
|
I use a little bit modified version of a method described here: https://stackoverflow.com/a/5311362/563175 So it looks like that:
where 24901 is parent's PID. It looks pretty ugly but does it's job perfectly. |
|||||||||
|
|
This is my version of killing all the child processes using bash script. It does not use recursion and depends on pgrep command. Use
Contents of killtrees.sh
|
|||
|
|
|
To add to Norman Ramsey's answer, it may be worth looking at at setsid if you want to create a process group.
Which I take to mean that you can create a group from the starting process. I used this in php in order to be able to kill a whole process tree after starting it. This may be a bad idea. I'd be interested in comments. |
|||||
|
|
If you know the pid of the thing you want to kill, you can usually go from the session id, and everything in the same session. I'd double check, but I used this for scripts starting rsyncs in loops that I want to die, and not start another (because of the loop) as it would if I'd just killall'd rsync.
If you don't know the pid you can still nest more
|
|||
|
|
|
if you have pstree and perl on your system, you can try this:
|
|||
|
|
|
Thanks for your wisdom, folks. My script was leaving some child processes on exit and the negation tip made things easier. I wrote this function to be used in other scripts if necessary:
Cheers. |
|||
|
|
chronosorherodescommands. – Michael Le Barbier Grünewald Sep 23 '14 at 16:18