Joshua Sutton
Joshua Sutton

Reputation: 141

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.

Upvotes: 13

Views: 17407

Answers (2)

okobaka
okobaka

Reputation: 616

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

Below i did mistake with ps switches, so above grep should be fine.


ah it is not working, ps could be use like this ps -ao pid,tty,comm -u $USER, this grep above should be fine ...

someone else is logged in ...

If so, add switch -u

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

$USER is user name that will be selected and listed, by default should be already set in OS environment. Multiple users could be selected by comma ps aux -u root,$USER

Take a note: If there is no specific username in the system, ps will throw ERROR: User name does not exist.

Read man ps for more.

-u userlist Select by effective user ID (EUID) or name. This selects the processes whose effective user name or ID is in userlist. The effective user ID describes the user whose file access permissions are used by the process (see geteuid(2)). Identical to U and --user.

Upvotes: 0

Vala
Vala

Reputation: 5674

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

Upvotes: 16

Related Questions