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

我现在的做法是:

[~]$ 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>

当前回答

我使用gkill processname,其中gkill是以下脚本:

cnt=`ps aux|grep $1| grep -v "grep" -c`
if [ "$cnt" -gt 0 ]
then
    echo "Found $cnt processes - killing them"
    ps aux|grep $1| grep -v "grep"| awk '{print $2}'| xargs kill
else
    echo "No processes found"
fi

注意:它不会杀死命令行中有“grep”的进程。

其他回答

killall -r regexp

-r, --regexp

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

如果你有pkill,

pkill -f csp_build.py

如果您只想根据进程名(而不是完整的参数列表)进行grep,则取消-f。

我用这个来杀死Firefox当它被脚本抨击和cpu抨击:) 把“Firefox”换成你想要的应用。我在Bash shell - OS X 10.9.3达尔文。

kill -Hup $(ps ux | grep Firefox | awk 'NR == 1 {next} {print $2}' | uniq | sort)

如果pkill -f csp_build.py没有杀死进程,你可以添加-9来发送一个杀死信号,该信号不会被忽略。即pkill -9 -f csp_build.py

在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命令,以杀死所有进程。