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

后端 未结 18 2783
孤独总比滥情好
孤独总比滥情好 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:49

    awk oneliner, which parses the header of ps output, so you don't need to care about column numbers (but column names). Support regex. For example, to kill all processes, which executable name (without path) contains word "firefox" try

    ps -fe | awk 'NR==1{for (i=1; i<=NF; i++) {if ($i=="COMMAND") Ncmd=i; else if ($i=="PID") Npid=i} if (!Ncmd || !Npid) {print "wrong or no header" > "/dev/stderr"; exit} }$Ncmd~"/"name"$"{print "killing "$Ncmd" with PID " $Npid; system("kill "$Npid)}' name=.*firefox.*
    

提交回复
热议问题