您的位置:首页 > 运维架构 > Linux

Linux之进程管理(2)相关命令之三

2016-09-11 17:20 696 查看
Linux之进程管理(2)相关命令之三(IPC)进程间通信及信号控制命令 kill killall
kill 命令kill - terminate a process,Inter Process Communication进程通信工具,默认为发送终止信号选项及用法:kill -l #显示所有信号CODE及名称kill [-SIGNAL] pid...#指定发送信号给对应pid的进程,不指定信号代码默认为15信号常见的信号:(注:可以使用man 7 signal打开帮助搜索 Standard Signals关键字)1) SIGHUP: 无须关闭进程而让其重读配置文件2) SIGINT: 中止正在运行的进程;相当于Ctrl+c9) SIGKILL: 杀死正在运行的进程15) SIGTERM:终止正在运行的进程(默认信号)18) SIGCONT:继续运行19) SIGSTOP:后台休眠使用方法,指定一个信号,如:信号号码:kill -1 932信号名称:kill -SIGHUP 932信号名称简写:kill -HUP 932使用案例:1、杀死真正打开的vim进程#查看当前打开vim的pid
[root@localhost ~]# ps -C vim -o pid= | tr -cd '[0-9]\n'
3042
#对当前vim进程发送15信号
[root@localhost ~]# kill -TERM 3042
#终端另一方已经被退出了vim
Vim: Caught deadly signal TERM0,0-1         All
Vim: Finished.
Terminated

2、不重启服务下重读httpd服务配置#查看当前httpd服务器状态,未启动
[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)
解析:这里使用centos7,dead表示未启动,centos6版本使用命令行servic httpd status,显示running表示启动,显示stopped...表示未启动。#启动httpd服务器
[root@localhost ~]# systemctl start httpd
#查看80端口是否已经开启,grep能匹配80端口出来表示服务以及启动
[root@localhost ~]# ss -tan | grep ':80\>'
LISTEN     0      128         :::80                      :::*
#查看当前默认首页配置(在httpd主配置文件中搜索DocumentRoot)
[root@localhost ~]# grep '^[[:space:]]*DocumentRoot' /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/html"
#查看对应的服务器根访问点是否有网页
[root@localhost ~]# ls /var/www/html/
index.html
解析:默认说明是对的,那么此时通过其它工具访问是可以显示html中的首页的。也就是index.html页面。#下面通过另外一个主机访问,直接通过http协议下载工具下载服务器的index.html页面
[root@mzf ~]# wget http://10.1.249.223/index.html --2016-08-17 19:33:15--  http://10.1.249.223/index.html Connecting to 10.1.249.223:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 109 [text/html]
Saving to: “index.html.1”

100%[============================================================================================================>] 109         --.-K/s   in 0s

2016-08-17 19:33:15 (1.09 MB/s) - “index.html.1” saved [109/109]
解析:出现下面链接完成的进度条表示已经可以访问,下面查看是否下载到了web页面。#查看当前目录是否存在刚刚下载的index.html
[root@mzf ~]# ls -l ./index.html
-rw-r--r--. 1 root root 100 Sep  7  2016 ./index.html
#查看文件内容
[root@mzf ~]# cat index.html
<html>
<head>
<title>Hello world</title>
</head/>
<body>
<h1>This is a http.</h1>
</body>
</html>
解析:现在服务端的 httpd服务正常运行,而且配置读取成功,下面我们修改httpd主配置文件中指定的DocumentRooot目录。这里我们记住当前index.html的title为Hello world。 #修改httpd.conf文件,改变DocumentRoot指定的目录

说明:此时Httpd服务首页更目录已经改成了/var/ww/myhtml目录,下面进行配置文件重读。#查看/var/www/myhtml有什么文件
[root@localhost ~]# ls /var/www/myhtml/
index.html
#查看myhtml目录下的index文件内容
[root@localhost ~]# cat /var/www/myhtml/index.html
<html>
<head>
<title>server.mzf.com<title>
</head>
<body>
<h1>This is /var/www/myhtml dir</h1>
</body>
</html>
说明:这里注意到这个文件的title为server.mzf.com,下面重读配置文件。#先查看此时httpd的主进程pid
[root@localhost ~]# pgrep  '^httpd\>' | sort -n | head -n 1
3069
#发送SIGHUP(1)信号来通知httpd服务进程重读配置
[root@localhost ~]# kill -SIGHUP 3069
#再次使用另外一台主机去通过httpd协议软件下载此时myhtml中的index.html文件
[root@mzf ~]# wget http://10.1.249.223/index.html --2016-08-17 19:47:41--  http://10.1.249.223/index.html Connecting to 10.1.249.223:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 111 [text/html]
Saving to: “index.html.2”

100%[============================================================================================================>] 111         --.-K/s   in 0s

2016-08-17 19:47:41 (1.06 MB/s) - “index.html.2” saved [111/111]

--2016-08-17 19:47:41--  http://./index2.html Resolving .... failed: Temporary failure in name resolution.
wget: unable to resolve host address “.”
FINISHED --2016-08-17 19:47:41--
Downloaded: 1 files, 111 in 0s (1.06 MB/s)
解析:这里显示下载成功,而且因为检查目录有同名文件,所有会自动存到当前目录中的文件名称为index.html.2,注意蓝色字那一行。 #于是查看index2.html信息title标签是否为server.mzf.com
[root@mzf ~]# cat index.html.2
<html>
<head>
<title>server.mzf.com<title>
</head>
<body>
<h1>This is /var/www/myhtml dir</h1>
</body>
</html>
解析:这里就说了httpd服务以及成功接收了SIGHUP信号。 总结:这里除了可以去强制杀死进程(发送SIGKILL信号),那么以后对某些支持SIGHUP信号的服务类进程进行修改配置,让其生效也不需要再重启整个服务了,只需要发送SIGHUP信号来让服务进程组进行重读进程参数。 killaill 命令killall - kill processes by name根据指定的进程名来杀死对应所有的进程命令格式及选项:killall -l :列出所有信行列表killall [options]... name ... -r [regx] :使用扩展正则表达式来匹配进程名称 -s, --signal [SIGNAL] :发送指定信行 -I:忽略进程名大小写 -q, --quiet :静默模式 -u USERNAME:只杀死指定进程所有者的进程 -i,--interactive:交互式案例(这里打开两个终端pts/0用于打开测试进程,pts/1用于进行进程处理):1、杀死vim进程#使用vim在另一个终端打开一个文件
[root@localhost ~]# vim file
#查看此当前终端vim的进程列表
[root@localhost ~]# ps -C vim -o pid,comm,ruser,tty
PID COMMAND         RUSER    TT
3296 vim            root     pts/0
#取出进程号
[root@localhost ~]# ps -C vim -o pid= | tr -cd '[0-9]\n'
3296
#对此vim进程发送终止信行TERM
[root@localhost ~]# killall -TERM -r '^vim\>'
#再次查看刚才打开vim的终端,已经vim进程已经终止了
Vim: Caught deadly signal TERM 0,0-1         All
Vim: Finished.
Terminated
注意:这里使用扩展正则表达式只是对发起进程的命令名称,而不是完整的命令名+参数,所有这里只需要针对命令命令就行了。 2、杀死管道进程tar#再次在刚才打开vim的终端上测试,使用tar命令测试
[root@localhost ~]# tar -cvf - /* | tar -tvf -
#然后切换到处理的终端,查看刚才终端的tar进程信息
[root@localhost ~]# pgrep -f 'tar.*-.*' -l -t pts/0
3319 tar
3320 tar
解析:为什么为匹配出2行,因为tar -cvf - /* | tar -tvf - 这中间虽然用管道相连,但是在终端来看,实际上开启了2个进程,及一个模拟打包,一个模拟查看列出。#使用killall直接杀死tar的进程
[root@localhost ~]# killall -r '^tar\>' -9
3、杀死后台进程#切换到测试开开进程的pts/0终端,运行后台查找命令
[root@localhost ~]# find / -type d -exec echo {} > /tmp/shfilelist.log \; &
[1] 83484
#查看其作业编号
[root@localhost ~]# jobs
[1]+  Running                 find / -type d -exec echo {} \; > /tmp/shfilelist.log &
#暂停后台进程
[root@localhost ~]# killall -19 find
[root@localhost ~]# jobs
[1]+  Stopped                 find / -type d -exec echo {} \; > /tmp/shfilelist.log
#继续运行
[root@localhost ~]# killall -18 find
#直接杀死
[root@localhost ~]# killall -9 -r '^\<find'
[root@localhost ~]# jobs
[1]+  Killed                  find / -type d -exec echo {} \; > /tmp/shfilelist.log
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  kill signal killall