4

I have a script running in a server and it will create many sub-processes( around 800 ). I want to kill them all in one stretch. Below is the ps information.

root     26363  0.0  0.0 119216  1464 ?        Ss   Mar02   0:00 SCREEN -S website_status
root     26365  0.0  0.0 108472  1844 pts/12   Ss   Mar02   0:00  \_ /bin/bash
root      4910  0.0  0.0 161684  1956 pts/12   S    Mar02   0:00      \_ su webmon
webmon    4939  0.0  0.0 108472  1924 pts/12   S+   Mar02   0:00          \_ bash
webmon    1094  3.4  0.0 107256  2432 pts/12   S    05:37   2:26              \_ sh /home/webmon/scripts/for_html/website/website_status.sh
webmon    5159  0.0  0.0 100956  1288 pts/12   S    05:37   0:00                  \_ mysql -vvv -h 192.168.12.38 -uwebmon -px xxxxxxxxxxxxx -e show processlist;
webmon    5160  0.0  0.0 103252   816 pts/12   S    05:37   0:00                  \_ grep in set
webmon    5161  0.0  0.0 105952   900 pts/12   S    05:37   0:00                  \_ awk {print $1}
webmon   12094  0.0  0.0 100956  1288 pts/12   S    05:37   0:00                  \_ mysql -vvv -h 192.168.12.38 -uwebmon -px xxxxxxxxxxxxx -e show processlist;
webmon   12095  0.0  0.0 103252   820 pts/12   S    05:37   0:00                  \_ grep Sleep -c
webmon   15044  0.0  0.0  60240  3004 pts/12   S    05:37   0:00                  \_ ssh -q 192.168.12.38 uptime | grep -o load.* | cut -f2 -d:
webmon   15166  0.0  0.0 100956  1292 pts/12   S    05:37   0:00                  \_ mysql -vvv -h 192.168.12.38 -uwebmon -px xxxxxxxxxxxxx -e show processlist;
webmon   15167  0.0  0.0 103252   816 pts/12   S    05:37   0:00                  \_ grep in set
webmon   15168  0.0  0.0 105952   900 pts/12   S    05:37   0:00                  \_ awk {print $1}
webmon   18484  0.0  0.0 100956  1288 pts/12   S    05:38   0:00                  \_ mysql -vvv -h 192.168.12.38 -uwebmon -px xxxxxxxxxxxxx -e show processlist;
webmon   18485  0.0  0.0 103252   816 pts/12   S    05:38   0:00                  \_ grep in set
webmon   18486  0.0  0.0 105952   900 pts/12   S    05:38   0:00                  \_ awk {print $1}
webmon   25110  0.0  0.0  60240  3008 pts/12   S    05:38   0:00                  \_ ssh -q 192.168.12.38 uptime | grep -o load.* | cut -f2 -d:
webmon    2598  0.0  0.0 100956  1292 pts/12   S    05:38   0:00                  \_ mysql -vvv -h 192.168.12.38 -uwebmon -px xxxxxxxxxxxxx -e show processlist;
webmon    2599  0.0  0.0 103252   816 pts/12   S    05:38   0:00                  \_ grep in set
webmon    2600  0.0  0.0 105952   900 pts/12   S    05:38   0:00                  \_ awk {print $1}

Killing of script only didn't work out, what is the best and fastest way if I have many sub-process here?

2

Have you tried pkill -signal -P ppid?

From the pkill manual:

pkill - look up or signal processes based on name and other attributes

-signal Defines the signal to send to each matched process

-P ppid Only match processes whose parent process ID is listed

If you wanted to kill 2432, and all its children, you should first try pkill -15 -P 2432, and if that doesn't work and you're willing to use the nuclear option: pkill -9 -P 2432.

1

I made a little research and I think little explanation will help others.

So as @Schives answered, with PPID we will be able to terminate the all child and the parent process.

But how to get the PPID easily?

ps -o pid,ppid,sess,cmd -U webmon

or

root@87-109:~$ ps -o pid,ppid,sess,cmd -U webmon | grep 'website_status.sh\|PID'
  PID  PPID   SESS CMD
16848 16787  16787 sh website_status.sh
17667  4405  4405 grep --color=auto website_status.sh\|PID
root@87-109:~$

then, pkill -15 -P 16787

where webmon is the user running the process.

I came across other ways to kill parent and child process in a blog, I am pasting them here.

This requires PID,PPID,PGID,GID and how to get them?

ps -o pid,ppid,pgid,gid,sess,cmd -U username

1. kill a group of processes with negative PID(Process ID)

kill  -TERM -PID

Is to kill PID and all its child processes.

2. kill a group of processes with their PGID(Process Group ID)

kill -- -$PGID   Kill using the default signal (TERM = 15)
kill -9 -$PGID   Kill using the KILL signal (9)

3. kill a group processes with only PID info

kill -- -$(ps -o pgid= $PID | grep -o [0-9]*)

Actually, you may notice that it's just the way from #2

4. Using pkill, kill processes by PGID(Proess Group ID)

pkill -9 -g $PGID

5. Using pkill, kill processes by GID(Group ID)

pkill -9 -G $GID

6. Using pkill, kill processes by PPID(Parent Process ID)

pkill -9 -p $PPID

7. Using pkill, kill processes by terminal

pkill -9 -t $terminal

Note: without /dev/ prefix

8. Using pkill, kill processes by process name

pkill -9 -x $process_name

9. Using pkill, kill processes by session

pkill -9 -s $sess
0

You can try to quit the screen session, at which point it should kill the child processes.

For example:

screen -X -S website_status quit
  • No. I don't want to kill the screen. – prado Mar 15 '17 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.