Score:0

How to kill 3 processes with totally different names?

in flag

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?

Nmath avatar
ng flag
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.
guiverc avatar
cn flag
Kill allows you to specify multiple PIDs; see `man kill` for the reference manual page.
Quasímodo avatar
jp flag
-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?
Score:8
fr flag

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)
melvio avatar
cn flag
Upvoted. @dawnslayer I propose that you mark this as the accepted answer. This solution will be the best approach for most people.
Score:4
cn flag

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 avatar
cn flag
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.
Peter Cordes avatar
fr flag
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`?
melvio avatar
cn flag
@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.
Peter Cordes avatar
fr flag
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.
melvio avatar
cn flag
I agree @PeterCordes. I have added a **warning** note. I'm also happy to accept additional edits that explain the tradeoffs/risks/caveats.
Score:2
my flag
killall name1 && killall name2 && killall name3
LiveWireBT avatar
fr flag
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 avatar
fr flag
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.
bac0n avatar
cn flag
`killall name1 name2 name3` will do.
Score:-1
us flag

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/ .*$//"`
LiveWireBT avatar
fr flag
A well known hack to avoid `grep 'pattern' | grep -v grep` is to use `grep '[p]attern'`. :-)
LiveWireBT avatar
fr flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.