How to kill all processes matching a name?

后端 未结 11 1513
不思量自难忘°
不思量自难忘° 2020-11-29 14:53

Say I want to kill every process containing the word amarok. I can print out the commands I want to execute. But how do I actually make the shell execute them. ie.



        
相关标签:
11条回答
  • 2020-11-29 15:04

    pkill -x matches the process name exactly.

    pkill -x amarok
    

    pkill -f is similar but allows a regular expression pattern.

    Note that pkill with no other parameters (e.g. -x, -f) will allow partial matches on process names. So "pkill amarok" would kill amarok, amarokBanana, bananaamarok, etc.

    I wish -x was the default behavior!

    0 讨论(0)
  • 2020-11-29 15:10

    If you're using cygwin or some minimal shell that lacks killall you can just use this script:

    killall.sh - Kill by process name.

    #/bin/bash
    ps -W | grep "$1" | awk '{print $1}' | xargs kill --
    

    Usage:

    $ killall <process name>
    
    0 讨论(0)
  • 2020-11-29 15:12

    From man 1 pkill

    -f     The pattern is normally only matched against the process name.
           When -f is set, the full command line is used.
    

    Which means, for example, if we see these lines in ps aux:

    apache   24268  0.0  2.6 388152 27116 ?        S    Jun13   0:10 /usr/sbin/httpd
    apache   24272  0.0  2.6 387944 27104 ?        S    Jun13   0:09 /usr/sbin/httpd
    apache   24319  0.0  2.6 387884 27316 ?        S    Jun15   0:04 /usr/sbin/httpd
    

    We can kill them all using the pkill -f option:

    pkill -f httpd
    
    0 讨论(0)
  • 2020-11-29 15:12

    The safe way to do this is:

    pkill -f amarok
    
    0 讨论(0)
  • 2020-11-29 15:14

    Maybe adding the commands to executable file, setting +x permission and then executing?

    ps aux | grep -ie amarok | awk '{print "kill -9 " $2}' > pk;chmod +x pk;./pk;rm pk
    
    0 讨论(0)
  • 2020-11-29 15:15

    use pgrep

    kill -9 $(pgrep amarok)
    
    0 讨论(0)
提交回复
热议问题