Source From Here
Question
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.
How-To
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 kill(1) command but instead of giving it a process number, give it the negation of the group number. For example to kill every process in group 5112, use kill -TERM -- -5112. Consider below demonstration shell script which help us to understand how it works more clear.
- test_sleep.sh
- test.sh
Let's execute test.sh:
So we can try to list process tree:
To kill test.sh and all its child process(s), do this way:
Question
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.
How-To
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 kill(1) command but instead of giving it a process number, give it the negation of the group number. For example to kill every process in group 5112, use kill -TERM -- -5112. Consider below demonstration shell script which help us to understand how it works more clear.
- test_sleep.sh
- #!/bin/sh
- echo "Sleep 10 minutes"
- sleep 600
- #!/bin/sh
- echo "Sleep 1"
- ./test_sleep.sh &
- S1=$!
- echo "Sleep 2"
- ./test_sleep.sh &
- S2=$!
- wait $S1 $S2
So we can try to list process tree:
To kill test.sh and all its child process(s), do this way:
沒有留言:
張貼留言