yea*_*yea 5 linux bash shell working-directory kill-process
我需要一种方法来杀死当前目录中正在运行的所有内容。我对 shell 有点陌生,所以请原谅我的愚蠢或可能不理解你的答案。我的理解还算不错,但如果您愿意花额外的时间来解释一下您对我的问题的解决方案到底是做什么的,我将不胜感激。
你必须使用lsof命令,那么参数将是你想要终止进程的目录
#!/usr/bin/env bash
lsof $(pwd) | \
awk -F " " ' { print $2 } ' | \
while read process ; do
[[ ${process} == "PID" ]] && continue;
# kill the processes here
# if you assign each process to a variable or an array
# you will not be able to access it after leaving this while loop
# pipes are executed as subshells
echo ${process};
done
Run Code Online (Sandbox Code Playgroud)