使用信号控制进程
kill(只能跟PID或JOB ID),killall(name),pgrep,pkill,top
给进程发送信号
[[email protected] ~]# 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 信号测试
[[email protected] ~]# yum -y install vsftpd 安装FTP服务
[[email protected] ~]# /etc/init.d/vsftpd restart 为 vsftpd 启动 vsftpd
[[email protected] ~]# /etc/init.d/vsftpd stop 关闭 vsftpd
[[email protected] ~]# ps aux |grep vsftpd
root 9160 0.0 0.0 52580 904 ? Ss 21:54 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
[[email protected] ~]# kill -1 9160 //发送重启信号,例如 vsftpd 的配置文件发生改变,希望重新加载
root 9160 0.0 0.0 52580 904 ? Ss 21:54 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
[[email protected] ~]# kill 9160 //发送停止信号,vsftpd 服务有停止的脚本 systemctl stop vsftpd(默认就是15)
[[email protected] ~]# ps aux |grep vsftpd
作业 2:crond 信号测试
//1 hup
[[email protected] ~]$ ps aux |grep crond
root 478 0.0 0.1 124144 1572 ? Ss 09:35 0:00 /usr/sbin/crond -n
[[email protected] ~]$ sudo kill -1 478
[[email protected] ~]$ ps aux |grep crond PID不变
root 478 0.0 0.1 124144 1572 ? Ss 09:35 0:00 /usr/sbin/crond -n
//15 term
[[email protected] ~]$ sudo kill 478
[[email protected] ~]$ ps aux |grep crond
[[email protected] ~]$ sudo systemctl start crond
[[email protected]s23gbf6Z ~]$ 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
[[email protected] ~]# 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
[[email protected] ~]# kill -19 2737
[[email protected] ~]# 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
[[email protected] ~]# kill -18 2737
[[email protected] ~]# 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:踢出一个从远程登录到本机的用户
[[email protected] ~]# w
15:56:09 up 2:16, 6 users, load average: 0.00, 0.00, 0.00
USER TTY FROM [email protected] 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
[[email protected] ~]# pkill -u hr01 结束用户hr01的进程
[[email protected] ~]# w
15:56:19 up 2:16, 5 users, load average: 0.00, 0.00, 0.00
USER TTY FROM [email protected] 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
[[email protected] ~]# w
15:58:16 up 2:18, 6 users, load average: 0.00, 0.00, 0.00
USER TTY FROM [email protected] 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
[[email protected] ~]# pkill -t pts/5 //终止 pts/5上所有进程
[[email protected] ~]# w
15:59:04 up 2:19, 6 users, load average: 0.00, 0.00, 0.00
USER TTY FROM [email protected] 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
[[email protected] ~]# pkill -9 -t pts/5 //终止 pts/5 上所有进程 并结束该 pts/5
[[email protected] ~]# w
16:00:09 up 2:20, 5 users, load average: 0.00, 0.00, 0.00
USER TTY FROM [email protected] 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
原文地址:http://blog.51cto.com/8450442/2325619