I am using the following command to kill one java process via shell script:
pidof java|xargs kill -9 $1
What if the list of pidof output has more than one elements? How do I kill them both with the same one command?
First of all, check out pkill. You can kill off any number of process given their name:
pkill java
You can even use the full command with arguments as part of the search
pkill -f some_string_in_arguemnts
Secondly, your construct with xargs will work just fine for multiple PID's as long as they are piped in as either space or newline separated numbers.
Kill with several process ids seems to be the best suitable solution for my purposes.
proctools package supplies them.
The killall command is available on most Linuces at any rate. You might have to install it on some distributions. It allows you to kill all processes that match the name provided on the command line.
pgrep can give you the list of PIDs
For example:
pgrep java
will give you the list of PIDs related to java. So you can use:
kill -9 `pgrep java`
I prefer
kill -9 `pidof java`
because of the inline output
> pgrep gulp
6316
6565
> pidof gulp
6565 6316
The problem with pkill, well you don't have it installed by default on some systems (like Ubuntu)