3

I have a question regarding killing processes that are running (and that I have started)

I have started many procesess and now I want to terminate them

doing

ps aux | grep aword

gives me the processes I want to kill. (aword being a word related to the processes) There are a lot so it takes me a while to kill them all

I would like to do it faster. Now a peculiarity of these processes is that some have as USER "theuser" and some have "root" I tried and it seems I have to kill first the ones with "theuser" because if I try the roots first , the process comes back to life.

So my question is how can I kill all the processes that come out of the grep command above but in the order that first the ones by "theuser" and then the ones with root (which require sudo of course)

1 Answers1

13

You better use pkill(alone) for that task:

  • Kill processes by name(partial/full match):

    pkill 'name'
    
  • Kill processes by name(exact match):

    pkill -x 'name'
    
  • Kill processes by full command line(partial/full match):

    pkill -f 'command'
    
  • Kill processes by full command line(exact match):

    pkill -xf 'command'
    

Run it without sudo to attempt to only kill matched processes "kill-able" by the invoking user or with sudo to attempt to kill all matched processes.


Notice: A dry-run(i.e. only print matched process IDs without killing them) can be done with changing pkill to pgrep or pgrep -l and pgrep -a(respectively, to also print process names and full command lines) ... For example:

pgrep 'name'

or:

pgrep -l 'name'

or:

pgrep -lx 'name'

or:

pgrep -lf 'command`

or:

pgrep -lxf 'command'

The same goes for pgrep -a as well.

Raffa
  • 34,963