我经常需要在编程期间终止一个进程。

我现在的做法是:

[~]$ ps aux | grep 'python csp_build.py'
user    5124  1.0  0.3 214588 13852 pts/4    Sl+  11:19   0:00 python csp_build.py
user    5373  0.0  0.0   8096   960 pts/6    S+   11:20   0:00 grep python csp_build.py
[~]$ kill 5124

如何自动提取进程id并在同一行中杀死它?

是这样的:

[~]$ ps aux | grep 'python csp_build.py' | kill <regex that returns the pid>

当前回答

给pkill -f

pkill -f /usr/local/bin/fritzcap.py

.py文件的确切路径为

# ps ax | grep fritzcap.py
 3076 pts/1    Sl     0:00 python -u /usr/local/bin/fritzcap.py -c -d -m

其他回答

这里有很多很好的答案-我使用了op接受的答案。只是添加了一个关于pkill和pgrep的小警告。正如你可能从他们的手册页看到的,在你的操作系统上,一些操作系统对进程名有15个字符的限制。-f选项绕过了我的操作系统,但我遇到了麻烦,直到我找到了这个选项!

在bash的一行中找到并杀死所有进程。

kill -9 $(ps -ef | grep '<exe_name>' | grep -v 'grep' | awk {'print $2'})

ps -ef | grep '<exe_name>' -给出匹配模式的运行进程详细信息(uname, pid等)的列表。输出列表还包括搜索它的grep命令。现在,对于kill,我们需要忽略这个grep命令过程。 ps -ef | grep' <exec_name>' | grep -v 'grep' -使用-v 'grep'添加另一个grep将删除当前的grep进程。 然后使用awk单独获取进程id。 然后将此命令保存在$(…)中,并将其传递给kill命令,以杀死所有进程。

killall -r regexp

-r, --regexp

将进程名模式解释为扩展正则表达式。

ps -o uid,pid,cmd|awk '{if($1=="username" && $3=="your command") print $2}'|xargs kill -15

从一个普通的PPID开始杀死我们自己的进程是相当频繁的,pkill与-P标志相关联对我来说是一个赢家。以@ghostdog74为例:

# sleep 30 &                                                                                                      
[1] 68849
# sleep 30 &
[2] 68879
# sleep 30 &
[3] 68897
# sleep 30 &
[4] 68900
# pkill -P $$                                                                                                         
[1]   Terminated              sleep 30
[2]   Terminated              sleep 30
[3]-  Terminated              sleep 30
[4]+  Terminated              sleep 30