2

I am using below script to find and kill process but its somehow not working. Please help to edit this if any flaw. I am greping JVM. Using AIX Machine.

    PID=`ps -eaf | grep JVM| grep -v grep | awk '{print $2}'`
    if [[ "" !=  "$PID" ]]
    then
        echo "killing $PID" 
        kill $PID
    else
        echo "PID not found"

    fi
RonyA
  • 585
  • 3
  • 11
  • 26
  • The `if` is better written as `if [[ "x" != "x$PID" ]]`, this ensures that `!=` is used for string comparison (as a bonus it also ensures that empty `grep` queries are handled correctly on different shells). – grochmal May 31 '16 at 19:52
  • The code looks basically correct - you could elaborate what you mean with "shomehow not working" but we will assume that the process survives the kill. If the answer from Neil Ellis doesn't help you would have to explain more. - What I do when grepping fix strings from ps output is like `ps -eaf | grep '[J]VM'` .. that does not require `grep -v grep` because the grep command does not match the regex – Stefan Hegny May 31 '16 at 20:07

1 Answers1

2

From the Wikipedia entry:

In Unix and Unix-like operating systems, kill is a command used to send a signal to a process. By default, the message sent is the termination signal, which requests that the process exit. But kill is something of a misnomer; the signal sent may have nothing to do with process killing.

So by default kill sends SIGTERM (equivalent to kill -15) you will probably need to do SIGKILL:

kill -9 $PID

or if you're been extra cautious or you need the system to shutdown gracefully then I recommend you use SIGINT as it is the same as Ctrl-C on the keyboard. So

kill -2 $PID

Java apps I'm afraid doesn't always handle SIGTERM correctly they rely upon good behaviour in the shutdown hooks. To make sure an app correctly handles signals like SIGTERM you can directly process the SIGTERM signal:

public class CatchTerm {
  public static void main(String[] args) throws Exception {
    Signal.handle(new Signal("TERM"), new SignalHandler () {
      public void handle(Signal sig) {
        //handle sigterm such as System.exit(1)

      }
    });
    Thread.sleep(86400000);
  }
}

For completeness here are the common signals

| Signal  | ID  | Action    | Description          | Java
| ---     | --- | ---       | ---                  | ---
| SIGHUP  | 1   | Terminate | Hangup               | The application should reload any config            
| SIGINT  | 2   | Terminate | Ctrl-C               | Keyboard interrupt, start clean shutdown
| SIGQUIT | 3   | Terminate | Terminal quit signal | JVM traps and issues a Thread dump       
| SIGABRT | 6   | Terminate | Process abort signal | Do not handle, quit immediately             
| SIGKILL | 9   | Terminate | Kill (forced)        | Cannot be trapped                      
| SIGTERM | 15  | Terminate | Termination signal.  | Quit quickly, safe but fast                

For more advanced process selection see killall and pkill:

Neil Ellis
  • 111
  • 1
  • 4
  • `SIGKILL` (`kill -9 ...`) is usually not recommended as it cannot be caught by the process, and no cleanup can be done. – Andreas Louv May 31 '16 at 19:57
  • Its Strange. I deleted the sh file and created it again with same script and it worked. Now i have one question as this script will capture only one pid for the jvm but after killing one pid , other pid is generating. How to modify this existing script to kill all the genertaed pid untill all get killed? – RonyA May 31 '16 at 20:04
  • @RishuA `killall JVM`? – Andreas Louv May 31 '16 at 20:06
  • 1
    Absolutely, however most Java processes contain poor signal handling. After 15 years of Java I haven't corrupted anything yet and most Java production environments I have seen also use -9. It's more a 'correctness' issue than an actual risk. To be a good citizen you could either use SIGINT or :- SIGINT, pause, SIGKILL. – Neil Ellis May 31 '16 at 20:06
  • @Rishua http://stackoverflow.com/questions/392022/best-way-to-kill-all-child-processes – Neil Ellis May 31 '16 at 20:08
  • Thank You All for your valuable time. I read what is posted here now. I got to conclusion that i should use for loop for the fix to kill the generated process. – RonyA May 31 '16 at 20:17