如何通过进程可执行文件的绝对文件路径来终止进程?因此,我想要杀死在给定位置从可执行文件创建的所有进程吗?
答案:
kill $(ps aux | grep '<absolute executable path>' | awk '{print $2}')
发布于 2022-03-28 22:03:10
答案:
kill $(pgrep -f /absolute/executable/path)
发布于 2017-11-29 19:54:23
你可以使用pkill(1) (或者killall(1).)
如果您正在编写一个程序,您可以考虑使用proc(5)。然后,您将在opendir(3)上循环readdir(3)的/proc/
目录(同时使用统计局(2),不要忘记克雷赛尔(3))。有病理病例(a 自移除程序)。
发布于 2019-04-11 23:51:09
function killpath {
ps aux | awk '{print $2"\t"$11}' | grep -E '^\d+\t'"$1"'$' | awk '{print $1}' | xargs kill -SIGTERM
}
用法:
killpath /Applications/Waterfox.app/Contents/MacOS/waterfox
它所做的:
ps aux
:列表过程awk '{print $2"\t"$11}'
:获取列2
(PID)和11
(可执行),并通过\t
选项卡字符将它们分隔开grep -E '^\d+\t'"$1"'$'
:匹配正则表达式^
:行的开头\d+
:一个或多个数字\t
:前面介绍的制表符"$1"
函数的输入,例如/Applications/Waterfox.app/Contents/MacOS/waterfox
$
:行尾
awk '{print $1}'
:只获取1
st列,即进程ID。xargs
:将换行符转换为空格以下是对数据的看法:
# killpath /Applications/Waterfox.app/Contents/MacOS/waterfox
# ps aux
# USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
luckydonald 23265 04,6 10,3 9222008 1736020 ?? S Sat10am 45:48.77 /Applications/Waterfox.app/Contents/MacOS/waterfox -foreground
luckydonald 23266 02,0 05,3 5743400 362344 ?? R Sat10am 11:52.52 /Applications/Waterfox.app/Contents/MacOS/waterfox -foreground
luckydonald 42128 04,5 00,2 4337884 35608 s002 S+ 1:17am 0:06.84 /usr/local/Cellar/docker-compose/1.23.2/libexec/bin/python3.7 /usr/local/bin/docker-compose logs -f --tail 100 r2tg
# awk '{print $2"\t"$11}'
23265 /Applications/Waterfox.app/Contents/MacOS/waterfox
23266 /Applications/Waterfox.app/Contents/MacOS/waterfox
42128 /usr/local/Cellar/docker-compose/1.23.2/libexec/bin/python3.7
# 1="/Applications/Waterfox.app/Contents/MacOS/waterfox"
# grep -E '^\d+\t'"$1"'$'
23265 /Applications/Waterfox.app/Contents/MacOS/waterfox
23266 /Applications/Waterfox.app/Contents/MacOS/waterfox
# awk '{print $1}'
23265
23266
# xargs
23265 23266
# xargs kill -SIGTERM
kill -SIGTERM 23265 23266
https://stackoverflow.com/questions/47560667
复制相似问题