每天一点Linux-11信号
使用信号控制进程
kill,killall,pkill,top
killall -数字 只能跟进程名(不能跟进程id)
给进程发送信号
[yang@ecs-ea9d ~]$ kill -l //列出所有支持的信号
编号) 信号名
1) SIGHUP 重新加载配置
2) SIGINT 键盘中断^C
3) SIGQUIT 键盘退出
9) SIGKILL 强制终止
15) SIGTERM 终止(正常结束),缺省信号(默认为15)
18) SIGCONT 继续(continue)
19) SIGSTOP 停止 (stop)
20)SIGTSTP 暂停^Z
示例1: 给crond进程发送信号1,15
//1
[yang@ecs-ea9d ~]$ ps aux | grep crond
root 498 0.0 0.0 126232 1596 ? Ss Aug14 0:00 /usr/sbin/crond -n
yang 6609 0.0 0.0 112660 968 pts/1 R+ 10:34 0:00 grep –color=auto crond
[yang@ecs-ea9d ~]$ sudo kill -1 498
[yang@ecs-ea9d ~]$ ps aux |grep crond
root 498 0.0 0.0 126232 1596 ? Ss Aug14 0:00 /usr/sbin/crond -n
//15
[yang@ecs-ea9d ~]$ sudo kill 498
[yang@ecs-ea9d ~]$ ps aux | grep crond
yang 6650 0.0 0.0 112660 968 pts/1 S+ 10:36 0:00 grep –color=auto crond
[yang@ecs-ea9d ~]$ ps aux |grep crond
[yang@ecs-ea9d ~]$ sudo systemctl start crond //重新启动crond进程
[yang@ecs-ea9d ~]$ ps aux |grep crond
root 6685 0.0 0.0 126236 1584 ? Ss 10:39 0:00 /usr/sbin/crond -n
实例2:信号测试9,15
[yang@ecs-ea9d ~]$ touch file1 file2
[yang@ecs-ea9d ~]$ tty
/dev/pts/1
[yang@ecs-ea9d ~]$ vim file1
[yang@ecs-ea9d ~]$ tty
/dev/pts/2
[yang@ecs-ea9d ~]$ vim file2
[yang@ecs-ea9d ~]$ ps aux |grep vim
yang 7280 0.0 0.1 151488 5216 pts/0 S+ 11:02 0:00 vim file1
yang 7306 0.1 0.1 151484 5216 pts/1 S+ 11:02 0:00 vim file3
[yang@ecs-ea9d ~]$ sudo kill 7280
[yang@ecs-ea9d ~]$ sudo kill -9 7306 //不建议,会导致文件损坏
有些centos7精简版没有killall需要安装 sudo yum install psmisc
[yang@ecs-ea9d]$ killall vim //给所有vim进程发送信号,
实例3:信号测试18,19
[root@ecs-ea9d ~]# ps aux |grep sshd
root 5571 0.0 0.0 64064 1164 ? Ss 09:35 0:00 /usr/sbin/sshd
[root@ecs-ea9d ~]# kill -STOP 5571
[root@ecs-ea9d ~]# ps aux |grep sshd
root 5571 0.0 0.0 64064 1164 ? Ts 09:35 0:00 /usr/sbin/sshd
[root@ecs-ea9d ~]# kill -cont 5571
[root@ecs-ea9d ~]# ps aux |grep sshd
root 5571 0.0 0.0 64064 1164 ? Ss 09:35 0:00 /usr/sbin/sshd
[yang@ecs-ea9d ~]$ 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@ecs-ea9d ~]$
[yang@ecs-ea9d ~]$ sudo kill -19 22319
[yang@ecs-ea9d ~]$ 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@ecs-ea9d ~]$
[yang@ecs-ea9d ~]$ sudo kill -cont 22319
[yang@ecs-ea9d ~]$ 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:踢出一个从远程登录到本机的用户
man pkill (可以以终端或者用户为对象来发送信号)
[root@ecs-ea9d ~]# 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@ecs-ea9d ~]# pkill -u alice
[root@ecs-ea9d ~]# 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@ecs-ea9d ~]$ 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 X.X.X.X 15:00 21.00s 0.00s 0.00s -bash
yang pts/1 X.X.X.X 15:00 5.00s 0.00s 0.00s w
yang pts/2 X.X.X.X 12:04 13.00s 0.12s 0.02s vim file1
[yang@ecs-ea9d ~]$ pkill -t pts/2 //终止pts/2上所有进程
[yang@ecs-ea9d ~]$ pkill -9 -t pts/2 //终止pts/2上所有进程 并结束该pts/2
[yang@ecs-ea9d ~]$ 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 X.X.X.X 15:00 3:55 0.00s 0.00s -bash
yang pts/1 X.X.X.X 15:00 3.00s 0.01s 0.00s w
yang pts/2 X.X.X.X 15:20 3.00s 0.00s 0.00s -bash
[yang@ecs-ea9d ~]$ sudo pkill -u yang
在top中使用k键输入进程号,也可以中断进程。
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 2924854739@qq.com
文章标题:每天一点Linux-11信号
本文作者:DROBP
发布时间:2019-08-19, 20:24:33
最后更新:2019-08-19, 20:25:32
原始链接:https://DROBP.github.io/2019/08/19/每天一点Linux-11信号/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。