系统相关
首页 > 系统相关> > linux – 有没有办法杀死在某个目录中运行的所有进程?

linux – 有没有办法杀死在某个目录中运行的所有进程?

作者:互联网

我需要一种方法来杀死当前目录中运行的所有内容.我对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

标签:bash,shell,linux,current-dir,kill-process
来源: https://codeday.me/bug/20190710/1426877.html