20181203使用訊號控制程序
使用訊號控制程序
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]
[[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
[
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] ~]$ 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