Kill Processes – Best Way to Kill Processes Created by Bash Script

killlinuxprocessshell-script

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?

Best Answer

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.

Related Question