linux進程管理之信號控制

 

使用信號控制進程


====================================================================================
kill,killall,pkill,top
kill,killall,pkill:都能發送信號。nginx

kill 只能接進程號vim

killall 能接進程名稱bash

pkill 能夠刪除某個終端,或者某個用戶的進程測試

pkill -t pst/2spa

pkill -9 -t pst/2code

pkill -u test01blog

pkill -19 -u test01進程

 

給進程發送信號
[root@localhost ~]# kill -l //列出全部支持的信號
編號 信號名
1) SIGHUP 從新加載配置,信號發出以後,主進程的進程號不會發生變化,子進程進程號會發生變化。io

[root@ds2 conf.d]# ps -ef |grep nginx root 27618      1  0 18:16 ?        00:00:00 nginx: master process /opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf nginx 27619  27618  0 18:16 ?        00:00:00 nginx: worker process root 27754  18508  0 19:26 pts/1    00:00:00 grep --color=auto nginx [root@ds2 conf.d]# kill -1 27618 [root@ds2 conf.d]# ps -ef |grep nginx root 27618      1  0 18:16 ?        00:00:00 nginx: master process /opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf nginx 27755  27618  0 19:27 ?        00:00:00 nginx: worker process root 27757  18508  0 19:27 pts/1    00:00:00 grep --color=auto nginx

2) SIGINT 鍵盤中斷^Cast

[root@ds2 ~]# ps -ef |grep ping root 27786  18508  0 19:32 pts/1    00:00:00 ping 192.168.42.177 root 27788  27764  0 19:32 pts/0    00:00:00 grep --color=auto ping [root@ds2 ~]# kill -2 27786 [root@ds2 ~]# ps -ef |grep ping root 27790  27764  0 19:33 pts/0    00:00:00 grep --color=auto ping

3) SIGQUIT 鍵盤退出

9) SIGKILL 強制終止
15) SIGTERM 終止(正常結束),缺省信號
18) SIGCONT 繼續
19) SIGSTOP 中止
20)SIGTSTP 暫停^Z

做業1: 給vsftpd進程發送信號1,15
[root@localhost ~]# ps aux |grep vsftpd
root 9160 0.0 0.0 52580 904 ? Ss 21:54 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
[root@localhost ~]# kill -1 9160 //發送重啓信號
root 9160 0.0 0.0 52580 904 ? Ss 21:54 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

[root@localhost ~]# kill 9160 //發送中止信號  (默認就是15信號)
[root@localhost ~]# ps aux |grep vsftpd

//1
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ ps aux |grep crond
root 478 0.0 0.1 124144 1572 ? Ss 09:35 0:00 /usr/sbin/crond -n
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ sudo kill -1 478
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ ps aux |grep crond
root 478 0.0 0.1 124144 1572 ? Ss 09:35 0:00 /usr/sbin/crond -n

//15
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ sudo kill 478
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ ps aux |grep crond
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ sudo systemctl start crond
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ ps aux |grep crond
root 22319 0.0 0.1 124140 1548 ? Ss 14:54 0:00 /usr/sbin/crond -n

做業2:信號測試9,15
[root@localhost ~]# touch file1 file2
[root@localhost ~]# tty
/dev/pts/1
[root@localhost ~]# vim file1

[root@localhost ~]# tty
/dev/pts/2
[root@localhost ~]# vim file2

[root@localhost ~]# ps aux |grep vim
root 4362 0.0 0.2 11104 2888 pts/1 S+ 23:02 0:00 vim file1
root 4363 0.1 0.2 11068 2948 pts/2 S+ 23:02 0:00 vim file2

[root@localhost ~]# kill 4362
[root@localhost ~]# kill -9 4363

[root@localhost ~]# killall vim //給全部vim進程發送信號
[root@localhost ~]# killall httpd

做業3:信號測試18,19

[root@ds2 ~]# ps aux |grep nginx root 27854  0.0  0.1  48112  1144 ?        Ss   19:48   0:00 nginx: master process /opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf nginx 27855  0.0  0.2  48492  2168 ?        S    19:48   0:00 nginx: worker process root 27878  0.0  0.0 112728   972 pts/1    S+   19:49   0:00 grep --color=auto nginx  [root@ds2 ~]# kill -19 27854 [root@ds2 ~]# ps aux |grep nginx root 27854  0.0  0.1  48112  1144 ?        Ts   19:48   0:00 nginx: master process /opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf nginx 27855  0.0  0.2  48492  2168 ?        S    19:48   0:00 nginx: worker process root 27880  0.0  0.0 112728   972 pts/1    R+   19:50   0:00 grep --color=auto nginx  [root@ds2 ~]# kill -18 27854 [root@ds2 ~]# ps aux |grep nginx root 27854  0.0  0.1  48112  1144 ?        Ss   19:48   0:00 nginx: master process /opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf nginx 27855  0.0  0.2  48492  2168 ?        S    19:48   0:00 nginx: worker process root 27882  0.0  0.0 112728   972 pts/1    S+   19:50   0:00 grep --color=auto nginx

做業4:踢出一個從遠程登陸到本機的用戶[root@localhost ~]# pkill --helppkill: invalid option -- '-'Usage: pkill [-SIGNAL] [-fvx] [-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST] [-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] [PATTERN][root@localhost ~]# pkill -u alice[root@localhost ~]# w 15:46:44 up 2:19, 4 users, load average: 0.17, 0.12, 0.08USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot tty1 :0 21:32 ? 4:22 4:22 /usr/bin/Xorg :root pts/0 :0.0 15:46 0.00s 0.00s 0.00s wroot pts/3 172.16.8.100 15:46 2.00s 0.01s 0.00s sleep 50000[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ w 15:17:25 up 5:42, 3 users, load average: 0.00, 0.01, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATyang pts/0 123.120.22.32 15:00 21.00s 0.00s 0.00s -bashyang pts/1 123.120.22.32 15:00 5.00s 0.00s 0.00s wyang pts/2 123.120.22.32 12:04 13.00s 0.12s 0.02s vim file1[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ pkill -t pts/2 //終止pts/2上全部進程[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ pkill -9 -t pts/2 //終止pts/2上全部進程 並結束該pts/2[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ w 15:20:59 up 5:45, 3 users, load average: 0.00, 0.01, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATyang pts/0 123.120.22.32 15:00 3:55 0.00s 0.00s -bashyang pts/1 123.120.22.32 15:00 3.00s 0.01s 0.00s wyang pts/2 123.120.22.32 15:20 3.00s 0.00s 0.00s -bash[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ sudo pkill -u yang

相關文章
相關標籤/搜索