13

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.

1
  • 11
    kill -9 is a bit harsh, are you sure you need this kind of violence? :-) Jul 26, 2012 at 15:29

2 Answers 2

17

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

2
  • By the way the ps a (removing the "only yourself") , is not in the code. I mistakenly pasted this from old code. Jul 26, 2012 at 15:47
  • 4
    @jordanm Performance impact of using cat here will barely be measurable, let alone noticeable to the user. I'll take better readability any day. If it makes you feel better you can replace it with less instead, but $(< file) will confuse the vast majority of newbies.
    – Vala
    Jul 26, 2012 at 16:14
0
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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.