Jayesh Bhoi
Jayesh Bhoi

Reputation: 25865

Why I am not getting signal SIGKILL on kill -9 command in bash?

In bash script I handle different signal as follows:

#!/bin/bash

sighdl () 
{
  echo "signal caught"
  #do something
  exit 0
}

trap sighdl SIGKILL SIGINT SIGTERM

Above code handle signal properly for following activity:

  1. Ctrl+C
  2. kill pid
  3. pkill scriptname

For kill -9 pid it does not call sighdl. As per my understanding (if I am not wrong) kill -9 sends the SIGKILL signal.

Any idea?

Upvotes: 17

Views: 17000

Answers (2)

access_granted
access_granted

Reputation: 1907

Actually, you can trap SIGKILL in SunOS (Solaris), but not in Linux (validated in Red Hat).

In Linux you can kill the foreign process softly with

kill -15 $PID

Upvotes: 1

anubhava
anubhava

Reputation: 784868

You cannot do that. Yes 9 is SIGKILL and Unix system by design doesn't allow any script/program to trap SIGKILL due to security reasons. Otherwise any script can trap & ignore SIGKILL which will make impossible to terminate that script by OS.

Upvotes: 29

Related Questions