How do I run pkill when invoking the shell to execute a string of commands?… here is a solution to the problem.
How do I run pkill when invoking the shell to execute a string of commands?
To automate system administration tasks, I wrote down the following shell line of code:
bash -c 'pkill -TERM -f java; true'
The problem is that pkill kills bash immediately after the pkill
command executes, so subsequent commands have no chance to execute.
Except that they are divided into two lines:
bash -c 'pkill -TERM -f java'
bash -c 'true'
Are there any other workarounds?
Solution
If you want to kill all Java processes, just remove -f
:
bash -c 'pkill -TERM java; true'
If you really want to kill non-java processes, such as mplayer "jungle_gremlins_of_java.avi
“, the typical “solution” is to rewrite commands so that the pattern does not match itself:
bash -c 'pkill -TERM -f "[j]ava"; true'