Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Script, Kill process by pulling from PID file

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.

like image 806
Joshua Sutton Avatar asked Jul 26 '12 15:07

Joshua Sutton


People also ask

How do you kill a process with changing PID?

You can use ps to find the PID of the process, then pass that to kill : kill $(ps -C /usr/lib/gvfs/gvfsd-mtp -o pid=) The -C flag specifies a command name to search for in the list of processes, and the -o pid= option means ps will print only the PID. The result is passed as the only argument to kill .

How do you kill a process in a script?

Kill a process with Taskkill To stop a process by its ID, use taskkill /F /PID <PID> , such as taskkill /F /ID 312 7 if 3127 is the PID of the process that you want to kill. To stop a process by its name, use taskkill /IM <process-name> /F , for example taskkill /ID mspaint.exe /F .

What is kill 9 PID?

“ kill -9” command sends a kill signal to terminate any process immediately when attached with a PID or a processname. It is a forceful way to kill/terminate a or set of processes. “ kill -9 <pid> / <processname>” sends SIGKILL (9) — Kill signal. This signal cannot be handled (caught), ignored or blocked.


1 Answers

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).

like image 149
Thor84no Avatar answered Sep 18 '22 12:09

Thor84no