2

I'm trying to figure out how to get a list of pids of multiple processes:

pidof proc1

1234 4321 7665

pidof proc2

3312 445

and take all this pids, where there are multiple instances of multiple processes and combine them and then feed them all into the kill command. Since kill can take multiple pids as input, I was hoping I could do this without a for-each loop:

kill -SIGKILL echo $(pidof proc1; pidof proc2)

but I just get "bash: kill: echo: arguments must be process or job IDs" error message. I'm very new to bash scripting so I'm sure there's just something obvious I'm not thinking of.

3 Answers 3

3

Get rid of the echo. It's unnecessary and is the reason for the error message since the word echo isn't a PID.

kill -SIGKILL $(pidof hsd-tab; pidof firefox-browser)

Better, pidof can take multiple program names:

kill -SIGKILL $(pidof hsd-tab firefox-browser)

Still better, use pkill to kill programs by name. It's like kill + pidof.

pkill -SIGKILL hsd-tab firefox-browser

Try to avoid killing programs with -SIGKILL or -9 unless you absolutely have to. It doesn't give them a chance to shut down cleanly. It's almost always overkill and is a bad habit. I would go with:

pkill hsd-tab firefox-browser
Sign up to request clarification or add additional context in comments.

1 Comment

thank you! I thought I had tried that and thought it didn't work because it outputs "kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]" when running the command without the echo, but the processes are infact gone. How can I get it to not output that message?
1

On the face of it, you need to eliminate echo:

kill -SIGKILL $(pidof hsd-tab; pidof firefox-browser)

That should give you two (or more) PIDs separated by spaces. I believe kill was complaining about the echo, which is neither a PID nor a job ID (those look like %1, etc).

Comments

0

You don't need the echo. $() takes the stdout of the commands run already.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.