In bash,
you should be able to do:
kill $(ps aux | grep '[p]ython csp_build.py' | awk '{print $2}') 其中'[p]ython csp_build.py'是要查询进程的指令。
Details on its workings are as follows:
-
The
psgives you the list of all the processes. - (//ps列出所有的进程列表)
-
The
grepfilters that based on your search string,[p]is a trick to stop you picking up the actualgrepprocess itself. -
//意译:grep是一个管道命令,其数据来源是依托之前的ps的数据。[p]是为了严格控制字符串从[p]开始,是为了防止杀掉grep本身。
-
The
awkjust gives you the second field of each line, which is the PID. -
//译:awk则是列出grep指令执行结果的第二列,也就是pid.
-
The
$(x)construct means to executexthen take its output and put it on the command line. The output of thatpspipeline inside that construct above is the list of process IDs so you end up with a command likekill 1234 1122 7654. - //译:$(x)结构意味着要执行指令x,然后输出,并把它放在指令(command)行中。上述指令包含的ps管道指令的输出就是进程ID列表。所以你可以使用kill这种指令来杀掉他们。
本文介绍了一种在Bash环境下精确查找并杀死指定Python进程的方法。利用ps、grep和awk组合命令来准确获取目标进程ID,再通过kill命令进行终止。此方法能有效避免误杀其他无关进程。
349

被折叠的 条评论
为什么被折叠?



