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

后端 未结 18 2656
孤独总比滥情好
孤独总比滥情好 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
    pkill firefox
    

    More information: http://linux.about.com/library/cmd/blcmdl1_pkill.htm

    0 讨论(0)
  • 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.*
    
    0 讨论(0)
  • 2020-11-28 17:52

    You can kill processes by name with killall <name>

    killall sends a signal to all processes running any of the specified commands. If no signal name is specified, SIGTERM is sent.

    Signals can be specified either by name (e.g. -HUP or -SIGHUP ) or by number (e.g. -1) or by option -s.

    If the command name is not regular expression (option -r) and contains a slash (/), processes executing that particular file will be selected for killing, independent of their name.

    But if you don't see the process with ps aux, you probably won't have the right to kill it ...

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

    The easiest way to do is first check you are getting right process IDs with:

    pgrep -f [part_of_a_command]
    

    If the result is as expected. Go with:

    pkill -f [part_of_a_command]
    
    0 讨论(0)
  • 2020-11-28 17:54

    Using #killall command:

    #killall -9 <processname>
    
    0 讨论(0)
  • 2020-11-28 17:55
    ps aux | grep processname | cut -d' ' -f7 | xargs kill -9 $
    
    0 讨论(0)
提交回复
热议问题