【问题标题】:Killing all process from command line从命令行杀死所有进程
【发布时间】:2011-12-21 18:35:23
【问题描述】:

我想从命令行中杀死所有的 firefox 进程。

例如:

MacPro:huangr$ ps -x | grep 'firefox'
 4147 ttys000    0:00.00 (firefox-bin)
 4177 ttys000    0:00.00 (firefox-bin)
 4234 ttys000    0:00.00 (firefox-bin)
 4273 ttys000    0:00.00 (firefox-bin)
 4282 ttys000    0:00.00 (firefox-bin)
 4285 ttys000    0:00.00 (firefox-bin)
 4298 ttys000    0:00.00 (firefox-bin)
 4301 ttys000    0:00.00 (firefox-bin)
 4304 ttys000    0:00.00 (firefox-bin)
 4311 ttys000    0:00.00 (firefox-bin)
 4317 ttys000    0:00.00 (firefox-bin)
 4320 ttys000    0:00.00 (firefox-bin)
 4338 ttys000    0:00.00 (firefox-bin)
 4342 ttys000    0:00.00 (firefox-bin)
 4377 ttys000    0:03.85 /Applications/Firefox.app/Contents/MacOS/firefox-bin -no-remote -foreground
 4394 ttys000    0:05.54 /Applications/Firefox.app/Contents/MacOS/firefox-bin -no-remote -foreground
 4471 ttys000    0:06.08 /Applications/Firefox.app/Contents/MacOS/firefox-bin -no-remote -foreground
 4581 ttys002    0:04.92 /Applications/Firefox.app/Contents/MacOS/firefox-bin -no-remote -foreground
 4607 ttys002    0:04.33 /Applications/Firefox.app/Contents/MacOS/firefox-bin -no-remote -foreground
 4626 ttys002    0:05.04 /Applications/Firefox.app/Contents/MacOS/firefox-bin -no-remote -foreground

我想一枪杀死他们,有什么简单的方法吗? 谢谢。

【问题讨论】:

标签: macos unix command-line


【解决方案1】:
kill -9 $(ps -x | grep 'firefox' | awk '{print $1}')

应该这样做

【讨论】:

  • 这不适用于 OS X 10.8.5。这会导致错误kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
【解决方案2】:

这很好用。

ps -ef | grep '[f]irefox' | awk '{print $1}' | xargs kill -9 ;

ps -ef | awk '/[f]irefox/ {print $1}' | xargs kill -9 ;

【讨论】:

    【解决方案3】:

    旁注 -

    kill -9 是过度杀伤(不是双关语),因为它会阻止被杀死的进程运行清理(例如 atexit() 调用,例如 exit 和 _exit 之间的区别)。 Firefox 可能有问题,也可能没有问题,但一般来说,只有在普通的“kill”失败后才考虑尝试“kill -9”。

    【讨论】:

      【解决方案4】:

      killall firefox-bin

      killall -9 firefox-bin

      必要时

      【讨论】:

        【解决方案5】:

        应该这样做 -

        kill `awk '$4~/firefox/{print $1}' <(ps -x)`
        

        或者一般来说,将此函数添加到您的启动脚本中 -

        killp() {
        awk -v pname="$1" '$4~/pname/{print $1}' <(ps -e) | xargs kill
        }
        

        测试:

        [jaypal:~/Temp] sleep 100&
        [1] 52530
        [jaypal:~/Temp] sleep 100&
        [2] 52531
        [jaypal:~/Temp] killp sleep
        [1]-  Terminated: 15          sleep 100
        [2]+  Terminated: 15          sleep 100
        

        【讨论】:

          猜你喜欢
          • 2018-10-25
          • 2012-11-21
          • 2014-05-18
          • 1970-01-01
          • 1970-01-01
          • 2019-02-04
          • 2014-04-08
          • 1970-01-01
          相关资源
          最近更新 更多