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 Answers
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)

- 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
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 pid
s:
$ 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.

- 288
-
1Note 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
-
1Can 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 likegrep -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 combinegrep
+kill
functionality. – melvio Aug 15 '21 at 11:02 -
1Right 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
killall name1 && killall name2 && killall name3

- 28,763
-
2Anecdote: 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 -
9Hello 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
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/ .*$//"`

- 100,812

- 279
-
2A well known hack to avoid
grep 'pattern' | grep -v grep
is to usegrep '[p]attern'
. :-) – LiveWireBT Aug 14 '21 at 09:07 -
4It 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
man kill
for the reference manual page. – guiverc Aug 14 '21 at 06:36