Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a pkill when invoking a shell to execute a string of commands?

To automate a system administration task, I wrote down the following line of shell code:

bash -c 'pkill -TERM -f java; true'

The problem is that pkill kills the bash immediately after the pkill command executes, and therefore subsequent commands do not have a chance to execute.

Apart from splitting the them into two lines:

bash -c 'pkill -TERM -f java'
bash -c 'true'

Is there any other workaround?


1 Answers

If you want to kill all java processes, simply drop the -f:

bash -c 'pkill -TERM java; true'

If you really also want to kill non-java processes like mplayer "jungle_gremlins_of_java.avi", the typical "solution" is to rewrite the command so that the pattern doesn't match itself:

bash -c 'pkill -TERM -f "[j]ava"; true'
like image 134
that other guy Avatar answered Jan 20 '26 20:01

that other guy