Reputation: 625
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
Upvotes: 2
Views: 3326
Reputation: 111
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:
Upvotes: 2