2018年12月11日 星期二

[Linux 常見問題] What's the best way to send a signal to all members of a process group?

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: 
// -o format
// %p: PID, Process ID
// %r: PPID, Parent Process ID
// %y: TTY
// %x: TIME
// %c: Command

# ps x -o "%p %r %y %x %c "


Take below process information as example: 


# ps x -o "%p %r %y %x %c " | grep -P 'test|python'
5086 5086 pts/1 00:00:00 test.sh
5087 5086 pts/1 00:00:00 python
5088 5086 pts/1 00:00:00 python
5089 5086 pts/1 00:00:00 python
5090 5086 pts/1 00:00:00 python
5091 5086 pts/1 00:00:00 python
5092 5086 pts/1 00:00:00 python
5093 5086 pts/1 00:00:00 python
5094 5086 pts/1 00:00:00 python
5095 5086 pts/1 00:00:00 python
5096 5086 pts/1 00:00:00 python

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 5086, use kill -TERM -- -5086

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...