linux进程管理之信号控制

 

使用信号控制进程


====================================================================================
kill,killall,pkill,top


给进程发送信号
[root@localhost ~]# kill -l //列出所有支持的信号
编号 信号名
1) SIGHUP 重新加载配置
2) SIGINT 键盘中断^C
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 //发送停止信号
[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@localhost ~]# ps aux |grep sshd
root 5571 0.0 0.0 64064 1164 ? Ss 09:35 0:00 /usr/sbin/sshd

[root@localhost ~]# kill -STOP 5571
[root@localhost ~]# ps aux |grep sshd
root 5571 0.0 0.0 64064 1164 ? Ts 09:35 0:00 /usr/sbin/sshd

[root@localhost ~]# kill -cont 5571
[root@localhost ~]# ps aux |grep sshd
root 5571 0.0 0.0 64064 1164 ? Ss 09:35 0:00 /usr/sbin/sshd

[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ ps aux |grep crond
root 22319 0.0 0.1 124140 1568 ? Ss 14:54 0:00 /usr/sbin/crond -n
yang 22427 0.0 0.0 112648 964 pts/2 R+ 15:07 0:00 grep --color=auto crond
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ sudo kill -19 22319
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ ps aux |grep crond
root 22319 0.0 0.1 124140 1568 ? Ts 14:54 0:00 /usr/sbin/crond -n
yang 22431 0.0 0.0 112648 964 pts/2 R+ 15:07 0:00 grep --color=auto crond
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ sudo kill -cont 22319
[yang@iZm5eiwihahzq6ds23gbf6Z ~]$ ps aux |grep crond
root 22319 0.0 0.1 124140 1568 ? Ss 14:54 0:00 /usr/sbin/crond -n
yang 22436 0.0 0.0 112648 960 pts/2 R+ 15:08 0:00 grep --color=auto crond


作业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





上一篇:Linux利用重定向实现两个会话窗口的交互


下一篇:ansible远程执行命令和拷贝目录或者文件