使用信号控制进程

kill(只能跟PID或JOB ID),killall(name),pgrep,pkill,top
20181203使用信号控制进程_第1张图片
给进程发送信号
[root@tianyun ~]# kill -l //列出所有支持的信号
编号 信号名
1) SIGHUP 重新加载配置 PID 不变
2) SIGINT 键盘中断^C
3) SIGQUIT 键盘退出
9) SIGKILL 强制终止
15) SIGTERM 终止(正常结束),缺省信号,允许去释放资源
18) SIGCONT 继续
19) SIGSTOP 停止
20)SIGTSTP 暂停^Z
作业 1: 给 vsftpd 进程发送信号 1,15
vsftpd 信号测试
[root@tianyun ~]# yum -y install vsftpd 安装FTP服务
[root@dong ~]# /etc/init.d/vsftpd restart 为 vsftpd 启动 vsftpd
[root@dong ~]# /etc/init.d/vsftpd stop 关闭 vsftpd
[root@tianyun ~]# 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@tianyun ~]# kill -1 9160 //发送重启信号,例如 vsftpd 的配置文件发生改变,希望重新加载
root 9160 0.0 0.0 52580 904 ? Ss 21:54 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

[root@tianyun ~]# kill 9160 //发送停止信号,vsftpd 服务有停止的脚本 systemctl stop vsftpd(默认就是15)
[root@tianyun ~]# ps aux |grep vsftpd
作业 2:crond 信号测试
//1 hup
[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 PID不变
root 478 0.0 0.1 124144 1572 ? Ss 09:35 0:00 /usr/sbin/crond -n

//15 term
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ sudo kill 478
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ ps aux |grep crond

[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ sudo systemctl start crond
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ ps aux |grep crond 进程重启后PID改变
root 22319 0.0 0.1 124140 1548 ? Ss 14:54 0:00 /usr/sbin/crond -n

作业 3: 信号测试18,19
[root@dong ~]# ps aux |grep vsftpd
root 2737 0.0 0.0 52580 824 ? Ss 10:38 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
//19 stop
[root@dong ~]# kill -19 2737
[root@dong ~]# ps aux |grep vsftpd
root 2737 0.0 0.0 52580 824 ? Ts 10:38 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
root 2806 0.0 0.0 103256 844 pts/0 S+ 10:59 0:00 grep vsftpd
//18 cont
[root@dong ~]# kill -18 2737
[root@dong ~]# ps aux |grep vsftpd
root 2737 0.0 0.0 52580 824 ? Ss 10:38 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
root 2815 0.0 0.0 103256 844 pts/0 S+ 11:00 0:00 grep vsftpd

作业 4:踢出一个从远程登录到本机的用户
[root@dong ~]# w
15:56:09 up 2:16, 6 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.1.144 10:10 4:28m 0.72s 0.72s -bash
dong1 pts/1 192.168.1.144 10:10 5:39m 0.08s 0.08s -bash
root pts/2 192.168.1.144 12:12 3:21m 0.11s 0.11s -bash
dong1 pts/3 192.168.1.144 12:19 3:28m 0.08s 0.08s -bash
root pts/4 192.168.1.144 15:50 0.00s 0.15s 0.01s w
hr01 pts/5 192.168.1.144 15:50 5:44 0.04s 0.04s -bash

[root@dong ~]# pkill -u hr01 结束用户hr01的进程

[root@dong ~]# w
15:56:19 up 2:16, 5 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.1.144 10:10 4:28m 0.72s 0.72s -bash
dong1 pts/1 192.168.1.144 10:10 5:39m 0.08s 0.08s -bash
root pts/2 192.168.1.144 12:12 3:21m 0.11s 0.11s -bash
dong1 pts/3 192.168.1.144 12:19 3:28m 0.08s 0.08s -bash
root pts/4 192.168.1.144 15:50 0.00s 0.16s 0.00s w


[root@dong ~]# w
15:58:16 up 2:18, 6 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.1.144 10:10 4:30m 0.72s 0.72s -bash
dong1 pts/1 192.168.1.144 10:10 5:41m 0.08s 0.08s -bash
root pts/2 192.168.1.144 12:12 3:23m 0.11s 0.11s -bash
dong1 pts/3 192.168.1.144 12:19 3:30m 0.08s 0.08s -bash
root pts/4 192.168.1.144 15:50 0.00s 0.37s 0.21s w
dong1 pts/5 192.168.1.144 15:58 3.00s 0.05s 0.00s sleep 10000

[root@dong ~]# pkill -t pts/5 //终止 pts/5上所有进程
[root@dong ~]# w
15:59:04 up 2:19, 6 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.1.144 10:10 4:31m 0.72s 0.72s -bash
dong1 pts/1 192.168.1.144 10:10 5:42m 0.08s 0.08s -bash
root pts/2 192.168.1.144 12:12 3:24m 0.11s 0.11s -bash
dong1 pts/3 192.168.1.144 12:19 3:31m 0.08s 0.08s -bash
root pts/4 192.168.1.144 15:50 0.00s 0.17s 0.00s w
dong1 pts/5 192.168.1.144 15:58 51.00s 0.05s 0.05s -bash


[root@dong ~]# pkill -9 -t pts/5 //终止 pts/5 上所有进程 并结束该 pts/5
[root@dong ~]# w
16:00:09 up 2:20, 5 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.1.144 10:10 4:32m 0.72s 0.72s -bash
dong1 pts/1 192.168.1.144 10:10 5:43m 0.08s 0.08s -bash
root pts/2 192.168.1.144 12:12 3:25m 0.11s 0.11s -bash
dong1 pts/3 192.168.1.144 12:19 3:32m 0.08s 0.08s -bash
root pts/4 192.168.1.144 15:50 0.00s 0.20s 0.03s w