LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash Script, Kill proccess by pulling from PID file (https://www.linuxquestions.org/questions/linux-general-1/bash-script-kill-proccess-by-pulling-from-pid-file-4175418798/)

j0sh10 07-26-2012 10:21 AM

Bash Script, Kill proccess 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.

Thank you for your time.
Josh

dmdeb 07-26-2012 10:41 AM

Quote:

Originally Posted by j0sh10 (Post 4738641)
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.

Thank you for your time.
Josh

Just a silly question: If you don't want to see glassfish processes belonging to other users, then why do you use "ps aux" instead of just "ps ux"?

j0sh10 07-26-2012 10:46 AM

Not a silly question.....

this was pasted from old code.......
The ps a (removing the "only yourself") has since been removed.

dmdeb 07-26-2012 11:13 AM

Quote:

Originally Posted by j0sh10 (Post 4738662)
Not a silly question.....

this was pasted from old code.......
The ps a (removing the "only yourself") has since been removed.

Oh wow... and it still fetches the wrong PID at times? That's quite a surprise!

PTrenholme 07-26-2012 11:44 AM

If your distribution supports it, you might find the killall command solves your problem somewhat more easily.

<edit>
Here's a bit of the man killall output
Code:


KILLALL(1)                                      User Commands                                      KILLALL(1)

NAME
      killall - kill processes by name

SYNOPSIS
      killall [-Z,--context pattern] [-e,--exact] [-g,--process-group] [-i,--interactive] [-o,--older-than
      TIME] [-q,--quiet] [-r,--regexp] [-s,--signal signal] [-u,--user user] [-v,--verbose] [-w,--wait]
      [-y,--younger-than TIME] [-I,--ignore-case] [-V,--version] [--] name ...
      killall -l
      killall -V,--version

DESCRIPTION
      killall  sends  a  signal  to  all processes running any of the specified commands. If no signal name is
      specified, SIGTERM is sent.

      Signals can be specified either by name (e.g. -HUP or -SIGHUP ) or by number (e.g. -1) or by option -s.

      If the command name is not regular expression (option -r) and contains a slash (/), processes  executing
      that particular file will be selected for killing, independent of their name.

      killall  returns  a zero return code if at least one process has been killed for each listed command, or
      no commands were listed and at least one process matched the -u and -Z search criteria. killall  returns
      non-zero otherwise.

      A killall process never kills itself (but may kill other killall processes).

</edit>


All times are GMT -5. The time now is 03:01 PM.