Bash Script, Kill process by pulling from PID file

Joshua Sutton picture Joshua Sutton · Jul 26, 2012 · Viewed 16.8k times · Source

This is what I have right now in the bash script:

ps aux | grep glassfish | grep domain1 | gawk '{print $2}' | xargs kill -9

The problem with this is that if someone else is logged in and pulling something related to glassfish, it wil pull that PID as well. Thus resulting in killing the wrong PID.

So My question is how do I fix what I have to only pull the correct PID, and how do I rewrite it to pull the PID from the PID file that glassfish generates.

Answer

Thor84no picture Thor84no · Jul 26, 2012

Edit the script that starts glassfish and place something like echo $$ > /path/to/PID-file (this can contain ~ for home directory or some other mechanism like $USER to make user specific) on the line immediately following the line starting the process. You can then kill the correct process using kill $(cat /path/to/PID-file).