linux进程管理之信号控制

使用信号控制进程


====================================================================================
kill,killall,pkill,top
kill,killall,pkill:都能发送信号。

kill 只能接进程号

killall 能接进程名称

pkill 可以删除某个终端,或者某个用户的进程

pkill -t pst/2

pkill -9 -t pst/2

pkill -u test01

pkill -19 -u test01

给进程发送信号
[root@localhost ~]# kill -l //列出所有支持的信号
编号 信号名
1) SIGHUP 重新加载配置,信号发出之后,主进程的进程号不会发生变化,子进程进程号会发生变化。

[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 键盘中断^C

[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 --help
pkill: 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.08
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 :0 21:32 ? 4:22 4:22 /usr/bin/Xorg :
root pts/0 :0.0 15:46 0.00s 0.00s 0.00s w
root 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.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
yang pts/0 123.120.22.32 15:00 21.00s 0.00s 0.00s -bash
yang pts/1 123.120.22.32 15:00 5.00s 0.00s 0.00s w
yang 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.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
yang pts/0 123.120.22.32 15:00 3:55 0.00s 0.00s -bash
yang pts/1 123.120.22.32 15:00 3.00s 0.01s 0.00s w
yang pts/2 123.120.22.32 15:20 3.00s 0.00s 0.00s -bash
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ sudo pkill -u yang

原文地址:https://www.cnblogs.com/anttech/p/10597619.html