0

I want to kill 3 processes with all different names, nothing in common at all. How do I do this? grep cant capture them all because there is nothing in common, or is there a way to make it grab all them?

  • 4
    It's hard to answer a question without any details. For starters you can tell us the processes and why you can't just run three commands. – Nmath Aug 14 '21 at 06:33
  • 3
    Kill allows you to specify multiple PIDs; see man kill for the reference manual page. – guiverc Aug 14 '21 at 06:36
  • 10
    -1 I really don't get this. Even if every command that can kill could only kill a single process, all you would have to do is to issue the command three times, right? – Quasímodo Aug 14 '21 at 17:45

4 Answers4

8

An example for killing gnome-disks and gnome-system-monitor.

kill $(pidof gnome-disks gnome-system-monitor)

I'm sure we have this around here already in some form, because someone here must have told me something like this years ago.

You could make it prettier, but more complicated like this:

# Provide a white space separated list of program names to be killed
programs_to_kill="gnome-disks gnome-system-monitor"
kill $(pidof $programs_to_kill)
LiveWireBT
  • 28,763
  • Upvoted. @dawnslayer I propose that you mark this as the accepted answer. This solution will be the best approach for most people. – melvio Aug 15 '21 at 11:39
4

If you really want to use grepping, you can use pkill to get the process-ids (pids). pkill greps for some pattern in the process names and kills the found processes.

The syntax you can use is:

pkill 'process_name1|other_process|something_else'

You can use pgrep (process grep) to check what you are about to kill with pkill.
The syntax is very similar:

pgrep -l 'proces_name1|other_process|something_else'

For example, if I run the following command on my machine, I get a listing of matches with their pids:

$ pgrep -l 'clamd|dockerd|snapd'
1952 snapd
1989 clamd
2085 dockerd
2813 dockerd

Warning: If you actually know the full process names, please use LiveWireBT's answer of:

kill $(pidof process_name1 other_proces)

That way, ssh-agent and sshd won't be the victim when you run pkill ssh and didn't pay attention to what pgrep -l ssh said.

melvio
  • 288
  • 1
    Note that I personally would use @LiveWireBT answer. Grepping can be tricky and you might end up killing too few processes, or even worse, too many. However, since you mentioned grep, I added this answer to provide you with the functionality. – melvio Aug 14 '21 at 10:06
  • 1
    Can you anchor the patterns with ^foo$|^bar$ or something to force them to match the full process name, instead of just substrings which could easily match too much? Or pkill/pgrep option to require full-string matches like grep -x? – Peter Cordes Aug 15 '21 at 10:40
  • @PeterCordes comment shows precisely the issue with grep; it's riskier than needed if you already know the full names. If you know the full names of the process to kill, I wouldn't even bother with -x or ^process-name$. I would use @LiveWireBT's solution instead. Only use this if you want to combine grep+kill functionality. – melvio Aug 15 '21 at 11:02
  • 1
    Right sure, but if you want to post about grep at all, you can be specific about what the risks are and what makes it less convenient to use safely. – Peter Cordes Aug 15 '21 at 11:18
  • I agree @PeterCordes. I have added a warning note. I'm also happy to accept additional edits that explain the tradeoffs/risks/caveats. – melvio Aug 15 '21 at 11:29
2
killall name1 && killall name2 && killall name3
LiveWireBT
  • 28,763
  • 2
    Anecdote: In a job a few years ago we had to monitor java applications and restart them if necessary. So I used killall to kill the processes, which was fine. I restarted them as in the documentation we had. All fine. Until next day (or a few days more) when the second level support asked how I killed the processes, because the java application that is supposed to monitor the processes "died" and didn't come back. That application wasn't in our documentation however. The kind of dumb things people do. :-) (Poor understanding of the application platform, poor documentation.) – LiveWireBT Aug 14 '21 at 09:22
  • 9
    Hello new contributor. Please provide a complete answer. It may be of interest why you choose to abort if the first kill command is not successful, which is what chaining commands together with && does in bash. – LiveWireBT Aug 14 '21 at 09:34
  • 3
    killall name1 name2 name3 will do. –  Aug 14 '21 at 17:35
-1

Assuming you can select the three processes by name, then my solution would be:

kill `ps awx| grep -E '((ProcessNameA)|(ProcessNameB)|(ProcessNameC))'  |grep -v grep| sed "s/ .*$//"`
terdon
  • 100,812
  • 2
    A well known hack to avoid grep 'pattern' | grep -v grep is to use grep '[p]attern'. :-) – LiveWireBT Aug 14 '21 at 09:07
  • 4
    It probably gets the job done, but you should study more manpages of the tools available. I built similar things years ago which coworkers were afraid to touch. I know when I mangle too much through grep and sed, then I should take a look at awk or rethink the whole thing. shellcheck is a useful tool. Googles Shell Style Guide is also worth a read. – LiveWireBT Aug 14 '21 at 09:44