How can I kill a process by name instead of PID?

后端 未结 18 2655
孤独总比滥情好
孤独总比滥情好 2020-11-28 17:10

Sometimes when I try to start Firefox it says \"a Firefox process is already running\". So I have to do this:

jeremy@jeremy-desktop:~$ ps aux | grep firefox
         


        
相关标签:
18条回答
  • 2020-11-28 17:42

    Kill all processes having snippet in startup path. You can kill all apps started from some directory by for putting /directory/ as a snippet. This is quite usefull when you start several components for the same application from the same app directory.

    ps ax | grep <snippet> | grep -v grep | awk '{print $1}' | xargs kill
    

    * I would preffer pgrep if available

    0 讨论(0)
  • 2020-11-28 17:43

    A bit longer alternative:

    kill `pidof firefox`
    
    0 讨论(0)
  • 2020-11-28 17:43

    more correct would be:

    export pid=`ps aux | grep process_name | awk 'NR==1{print $2}' | cut -d' ' -f1`;kill -9 $pid
    
    0 讨论(0)
  • 2020-11-28 17:44

    On Mac I could not find the pgrep and pkill neither was killall working so wrote a simple one liner script:-

    export pid=`ps | grep process_name | awk 'NR==1{print $1}' | cut -d' ' -f1`;kill $pid
    

    If there's an easier way of doing this then please share.

    0 讨论(0)
  • 2020-11-28 17:46

    To kill with grep:

    kill -9 `pgrep myprocess`
    
    0 讨论(0)
  • kill -9 $(ps aux | grep -e myprocessname| awk '{ print $2 }')

    0 讨论(0)
提交回复
热议问题