user1688175
user1688175

Reputation:

Bash - Killing all process in a single command

I have the following bash command which works quite good:

ps aux | grep mpg321 | grep -v "grep mpg321" | awk '{print $2}' | xargs kill

The only problem is, when there is no mpg321 being executed it returns an awful message which I would not like to display to the user.

Is there a "quiet" parameter of a way to put an if in this statement to avoid returning the message?

Usage:
 kill [options] <pid> [...]

Options:
 <pid> [...]            send signal to every <pid> listed
 -<signal>, -s, --signal <signal>
                        specify the <signal> to be sent
 -l, --list=[<signal>]  list all signal names, or convert one to a name
 -L, --table            list all signal names in a nice table

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see kill(1).

Thank you!

Upvotes: 0

Views: 451

Answers (2)

Philippe Banwarth
Philippe Banwarth

Reputation: 17735

You can also use pkill as an alternative to killall

By the way xargs has a --no-run-if-empty option if you don't want to execute the command when there are no parameters to add.

Upvotes: 2

DevSolar
DevSolar

Reputation: 70273

How about killall?

killall mpg321

Upvotes: 4

Related Questions