Linux进程管理使用信号控制进程

发布于:2022-11-29 ⋅ 阅读:(708) ⋅ 点赞:(0)

目录

一、信号概念

1、信号的种类

2、测试9,15信号

3、测试18,19信号


一、信号概念

        信号全称为软中断信号,也有人称作软中断,信号机制是进程之间相互传递消息的一种方法。

1、信号的种类

        kill,killall:都能发送信号。

        kill 只能接进程号

        killall 能接进程名称

kill  -l   可查看所有支持信号

编号为1-31的为不可靠信号,34-64的为可靠信号。连续不间断发送信号,信号会丢失的为不可靠信号,连续不间断发送信号,信号不会丢失的为可靠信号。

简单介绍几个信号的功能

1)SIGHUP    重新加载配置
2)SIGINT       键盘中断Ctrl+C
3)SIGQUIT     键盘退出Ctrl+\,类似SIGINT
9)SIGKILL       强制终止,无条件
15)SIGTERM    终止(正常结束),缺省信号
18)SIGONT      继续
19)SIGSTOP     暂停
20)SIGTSTP     键盘停止Ctrl+Z

语法:kill  命令编号   进程id

1) SIGHUP 重新加载配置,信号发出之后,主进程的进程号不会发生变化,子进程进程号会发生变化。

[root@localhost ~]# ps -ef|grep kthreadd
root          2      0  0 08:55 ?        00:00:00 [kthreadd]
root       6395   4863  0 13:45 pts/0    00:00:00 grep --color=auto kthreadd

[root@localhost ~]# kill -1  2
[root@localhost ~]# ps -ef|grep kthreadd
root          2      0  0 08:55 ?        00:00:00 [kthreadd]
root       6420   4863  0 13:46 pts/0    00:00:00 grep --color=auto kthreadd

kthread的子进程号由6395变为6420

2、测试9,15信号

创建两个文件file1,file2
打开一个终端,查看当前终端(命令tty),用vim进行编辑file1

[root@localhost ~]# tty            //查看当前终端
/dev/pts/0
[root@localhost ~]# vim /test/file1


打开另一个终端,查看当前终端(命令tty),用vim编辑file2

[root@localhost ~]# tty                //查看当前终端
/dev/pts/1
[root@localhost ~]# vim /test/file2

打开多个终端方法如图:

 

 打开第三个终端查看vim进程

[root@localhost ~]# ps aux |grep  vim                //查看vim进程
root       6784  0.2  0.5 149808  5528 pts/0    S+   14:08   0:00 vim /test/file1
root       6803  0.6  0.5 149800  5512 pts/1    S+   14:08   0:00 vim /test/file2
root       6805  0.0  0.0 112824   976 pts/2    R+   14:08   0:00 grep --color=auto vim


使用9号信号对vim对file1编辑的进程进行操作

[root@localhost ~]# kill -9 6784        //vim对file1的编辑进程号为6484,使用9号信号进行操作

 vim对file1的操作进程被杀死

 使用15号信号对vim对file2编辑的进程进行操作(15号为默认的信号可以直接使用kill  进程号直接操作)

[root@localhost ~]# kill -15 6803(kill  6803)      //使用15号信号对进程6803进行操作

 vim对file2的编辑进程被终止

注意:一般不推荐使用9号信号,在进程僵死的时候可以使用9号信号,平时我们都使用15号信号关掉进程

 killall   vim                              //杀死所有vim进程

3、测试18,19信号

[root@localhost ~]# ps aux |grep nginx
root      27854  0.0  0.1  48112  1144 ?        Ss   14:29   0:00 nginx: master process /opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
nginx     27855  0.0  0.2  48492  2168 ?        S    14:29   0:00 nginx: worker process
root      27878  0.0  0.0 112728   972 pts/1    S+   14:29   0:00 grep --color=auto nginx
 
[root@localhost ~]# kill -19 27854
[root@localhost ~]# ps aux |grep nginx
root      27854  0.0  0.1  48112  1144 ?        Ts   14:30   0:00 nginx: master process /opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
nginx     27855  0.0  0.2  48492  2168 ?        S    14:30   0:00 nginx: worker process
root      27880  0.0  0.0 112728   972 pts/1    R+   14:30   0:00 grep --color=auto nginx

[root@localhost ~]# kill -18 27854
[root@localhost ~]# ps aux |grep nginx
root      27854  0.0  0.1  48112  1144 ?        Ss   14:30   0:00 nginx: master process /opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
nginx     27855  0.0  0.2  48492  2168 ?        S    14:30   0:00 nginx: worker process
root      27882  0.0  0.0 112728   972 pts/1    S+   14:30   0:00 grep --color=auto nginx

你的点赞是对我的激励!!!

你的评论是对我的勉励!!!

你的收藏是对我的认可!!!

你的关注是我们之间距离的增进!!!


网站公告

今日签到

点亮在社区的每一天
去签到