linux中kill和ps命令,Linux中ps配合Kill过程的N种方式

惯例篇:

首先,用ps查看进程,办法如下:

$ ps -ef

……

smx 1822 10 11:38 ? 00:00:49 gnome-terminal

smx 182318220 11:38 ? 00:00:00 gnome-pty-helper

smx 182418220 11:38 pts/0 00:00:02 bash

smx 1827 14 11:38 ? 00:26:28 /usr/lib/firefox-3.6.18/firefox-bin

smx 185718220 11:38 pts/1 00:00:00 bash

smx 188016190 11:38 ? 00:00:00 update-notifier

……

smx 1194618240 21:41 pts/0 00:00:00 ps -ef

或者:

$ ps -aux

……

smx 18220.10.858484 18152 ? Sl 11:38 0:49 gnome-terminal

smx 18230.00.0 1988 712 ? S 11:38 0:00 gnome-pty-helper

smx 18240,www.9zq0og1.com.00.1 68203776 pts/0 Ss 11:38 0:02 bash

smx 18274.35.8 398196 119568 ? Sl 11:3826:13 /usr/lib/firefox-3.6.18/firefox-bin

smx 18570.00.1 66883644 pts/1 Ss 11:38 0:00 bash

smx 18800.00.641536 12620 ? S 11:38 0:00 update-notifier

……

smx 119530.00.0 27161064 pts/0 R+ 21:42 0:00 ps -aux

此时假如我想杀了火狐的进程就在终端输入:

$ kill -s 9 1827

其中-s 9 制订了传递给进程的信号是9,即强迫、尽快终止进程。各个终止信号及其作用见附录。

1827则是上面ps查到的火狐的PID。

简略吧,但有个问题,进程少了则无所谓,进程多了,就会感到苦楚了,无论是ps -ef 仍是ps -aux,每次都要在一大串过程信息里面查找到要杀的进程,看的眼都花了。

进阶篇:

改进1:

把ps的查询结果通过管道给grep查找包含特定字符串的进程。管道符“”用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入。

$ ps -ef grep firefox

smx 1827 14 11:38 ? 00:27:33 /usr/lib/firefox-3.6.18/firefox-bin

smx 1202918240 21:54 pts/0 00:00:00 grep --color=auto firefox

这次就清新了。而后就是

$kill -s 9 1827

还是嫌打字多?

改进2——使用pgrep:

一看到pgrep首先会想到什么?没错,grep!pgrep的p表明了这个命令是专门用于进程查问的grep。

$ pgrep firefox

1827

看到了什么?没错火狐的PID,接下来又要打字了:

$kill -s 9 1827

改进3——使用pidof:

看到pidof想到啥?没错pid of xx,字面翻译过来就是 xx的PID。

$ pidof firefox-bin

1827

和pgrep比拟稍显不足的是,pidof必需给出进程的全名。然后就是陈词滥调:

$kill -s 9 1827

无论应用ps 然后缓缓查找进程PID 还是用grep查找包括相应字符串的进程,亦或者用pgrep直接查找包含相应字符串的进程PID,然后手动输入给kill杀掉,都稍显麻烦。有不更便利的方式?有!

改进4:

$ps -ef grep firefox grep -v grep cut -c 9-15 xargs kill -s 9

解释:

“grep firefox”的输出结果是,所有含有症结字“firefox”的进程。

“grep -v grep”是在列出的进程中去除含有要害字“grep”的进程。

“cut -c 9-15”是截取输入行的第9个字符到第15个字符,而这正好是进程号PID。

“xargs kill -s 9”中的xargs命令是用来把前面命令的输出结果(PID)作为“kill -s 9”命令的参数,并执行该命令。“kill -s 9”会强行杀掉指定进程。

岂非你不想埋怨点什么?没错太长了

改进5:

晓得pgrep跟pidof两个命令,干嘛还要打那么长一串!

$ pgrep firefox xargs kill -s 9

改进6:

$ ps -ef grep firefox awk '{print $2}' xargs kill -9

kill: No such process

有一个比拟愁闷的处所,进程已经准确找到并且终止了,然而履行完却提醒找不到进程。

其中awk '{print $2}' 的作用就是打印(print)出第二列的内容。依据常规篇,可以知道ps输出的第二列正好是PID。就把进程相应的PID通过xargs传递给kill作参数,杀掉对应的进程。

改进7:

莫非每次都要调用xargs把PID传递给kill?谜底是否认的:

$kill -s 9 `ps -aux grep firefox awk '{print $2}'`

改良8:

没错,命令仍然有点长,怎么在centOS下实现翻qiang啊? - Linux体系治理 - ChinaUnix.ne,换成pgrep。

$kill -s 9 `pgrep firefox`

改进9——pkill:

看到pkill想到了什么?没错pgrep和kill!pkill=pgrep+kill。

$pkill -9 firefox

阐明:"-9" 即发送的信号是9,pkill与kill在这点的差异是:pkill毋庸 “s”,终止信号等级直接跟在 “-“ 后面。之前我始终认为是 "-s 9",成果每次运行都无奈终止进程。

改进10——killall:

killall和pkill是类似的,不外如果给出的进程名不完全,killall会报错。pkill或者pgrep只有给出进程名的一局部就能够终止进程。

$killall -9 firefox

附录:各种信号及其用处

SignalDescriptionSignal number on Linux x86[1]SIGABRTProcess aborted6SIGALRMSignal raised by alarm14SIGBUSBus error: "access to undefined portion of memory object"7SIGCHLDChild process terminated, stopped (or continued*)17SIGCONTContinue if stopped18SIGFPEFloating point exception: "erroneous arithmetic operation"8SIGHUPHangup1SIGILLIllegal instruction4SIGINTInterrupt2SIGKILLKill (terminate immediately)9SIGPIPEWrite to pipe with no one reading13SIGQUITQuit and dump core3SIGSEGVSegmentation violation11SIGSTOPStop executing temporarily19SIGTERMTermination (request to terminate)15SIGTSTPTerminal stop signal20SIGTTINBackground process attempting to read from tty ("in")21SIGTTOUBackground process attempting to write to tty ("out")22SIGUSR1User-defined 110SIGUSR2User-defined 212SIGPOLLPollable event29SIGPROFProfiling timer expired27SIGSYSBad syscall31SIGTRAPTrace/breakpoint trap5SIGURGUrgent data available on socket23SIGVTALRMSignal raised by timer counting virtual time: "virtual timer expired"26SIGXCPUCPU time limit exceeded24SIGXFSZFile size limit exceeded25

转自

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值