Tim51092
Tim51092

Reputation: 201

Concatenate output of multiple commands and use them as input to another command

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.

Upvotes: 2

Views: 472

Answers (3)

James R.
James R.

Reputation: 143

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

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 362147

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

Upvotes: 3

Jonathan Leffler
Jonathan Leffler

Reputation: 755064

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

Upvotes: 1

Related Questions