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

我现在的做法是:

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

当前回答

使用pgrep -可用于许多平台:

kill -9 `pgrep -f cps_build`

pgrep -f将返回所有符合"cps_build"的pid

其他回答

ps不需要用户开关。

kill `ps ax | grep 'python csp_build.py' | awk '{print $1}'`

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

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

我使用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 -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