To illustrate the example below, we first install the Apache server and then start Apache.
[root@localhost ~]# yum install httpd
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# ps aux | grep http
root 3716 0.5 0.0 221936 5000 ? Ss 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3717 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3718 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3719 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3720 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3722 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
root 3728 0.0 0.0 112648 960 pts/0 R+ 13:49 0:00 grep --color=auto http
[root@localhost ~]#
At this point, we see that Apache services have started a total of 3716, 3717, 3718, 3719, 3720, 3722 pid processes. The last line 3728 is quite special. This process is the process pid of ps aux.
In the following example, we filter out the process.
To get to the topic, we create a shell called tokill. The purpose of this script is to kill all process es associated with it according to the keywords entered by the user.
#!/bin/bash
#
#Ask what to stop using the kill command and then kill it
echo Which process do you want to kill?
read TOKILL
kill $(ps aux | grep $TOKILL | grep -v grep | awk '{ print $2 }')
When the shell enters the read TOKILL command, it stops waiting for the user's input and assigns the user's input to the variable TOKILL.
kill (psaux|grepTOKILL | grep -v grep | awk'{print $2}') is a bit more complicated, let's explain it one by one.
ps aux | grep http is a list of all processes that contain http keywords
[root@localhost ~]# ps aux | grep http
root 3716 0.5 0.0 221936 5000 ? Ss 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3717 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3718 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3719 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3720 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3722 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
root 3728 0.0 0.0 112648 960 pts/0 R+ 13:49 0:00 grep --color=auto http
[root@localhost ~]#
As I said before, the last line is not what we want. We're going to filter this line out.
[root@localhost ~]# ps aux | grep http | grep -v grep
root 3716 0.0 0.0 221936 5000 ? Ss 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3717 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3718 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3719 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3720 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
apache 3722 0.0 0.0 224020 3088 ? S 13:49 0:00 /usr/sbin/httpd -DFOREGROUND
Then we have to get all the process numbers (PID)
[root@localhost ~]# ps aux | grep http | grep -v grep | awk '{ print $2 }'
3716
3717
3718
3719
3720
3722
[root@localhost ~]#
awk is a tool for processing formatted data, such as the second column in the list we want to get
For more detailed usage of awk, please refer to http://www.cnblogs.com/wangqiguo/p/5863266.html
The last execution, called command substitution, is simply to execute the command in the command.
And the results of execution are provided to kill for use, that is, all PIDs are given to kill for use.
Now execute the script, and don't forget to pay the user permission to execute.
[root@localhost ~]# cd /usr/local/bin/
[root@localhost bin]# ll
total 4
lrwxrwxrwx. 1 root root 30 May 12 13:51 db2greg -> /opt/ibm/db2/V11.1/bin/db2greg
lrwxrwxrwx. 1 root root 32 May 12 13:51 db2ls -> /opt/ibm/db2/V11.1/install/db2ls
-rw-r--r--. 1 root root 192 Jun 17 13:39 tokill.sh
[root@localhost bin]# chmod +x tokill.sh
[root@localhost bin]# ./tokill.sh
Which process do you want to kill?
http
[root@localhost bin]# ps aux | grep http
root 4102 0.0 0.0 112648 956 pts/0 S+ 14:12 0:00 grep --color=auto http
[root@localhost bin]#
We can see that http process es have been kill ed.
But it's not over yet, if we execute tokill.sh once.
[root@localhost bin]# ./tokill.sh
Which process do you want to kill?
http
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
[root@localhost bin]#
We will find that the script we just executed is wrong again.
[root@localhost bin]# bash -x tokill.sh
+ echo Which process do you want to 'kill?'
Which process do you want to kill?
+ read TOKILL
http
++ ps aux
++ grep http
++ grep -v grep
++ awk '{ print $2 }'
+ kill
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
[root@localhost bin]#
We use bash-x to debug the code
Why did you find that something went wrong when executing kill?
We execute separately on the command line
[root@localhost bin]# ps aux | grep http |grep -v grep | awk '{print $2}'
[root@localhost bin]#
It is found that http process no longer exists, because the kill command needs a pid as a required parameter, so when the pid is empty, it will be wrong, how to solve this problem. And listen to the next decomposition.