Why I am not getting signal SIGKILL on kill -9 command in bash?
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.
Why is the KILL signal handler not executing when my child process dies
I couldn't find anything in the bash documentation that would explain the observed behavior, so I turned to the source code. Debugging lead to the function notify_of_job_status()
. The line that prints the message about a killed subprocess can be reached only if all of the following conditions hold:
- the subprocess is registered in the job table (i.e. has not been
disown
-ed) - the shell was NOT started in interactive mode
- the signal that terminated the child process is NOT trapped in the parent shell (see the
signal_is_trapped (termsig) == 0
check)
Demonstration:
$ cat test.sh
echo Starting a subprocess
LC_ALL=C sleep 100 &
Active_pid=$!
case "$1" in
disown) disown ;;
trapsigkill) trap "echo Signal SIGKILL caught" 9 ;;
esac
sleep 1
kill -9 $Active_pid
sleep 1
echo End of script
$ # Demonstrate the undesired message
$ bash test.sh
Starting a subprocess
test.sh: line 14: 15269 Killed LC_ALL=C sleep 100
End of script
$ # Suppress the undesired message by disowning the child process
$ bash test.sh disown
Starting a subprocess
End of script
$ # Suppress the undesired message by trapping SIGKILL in the parent shell
$ bash test.sh trapsigkill
Starting a subprocess
End of script
$ # Suppress the undesired message by using an interactive shell
$ bash -i test.sh
Starting a subprocess
End of script
How this removes the trace of the first test without executing
echo Signal SIGKILL
?
The trap is not executed since the KILL
signal is received by the sub-process rather than the shell process for which the trap has been set. The effect of the trap on the diagnostics is in the (somewhat arguable) logic in the notify_of_job_status()
function.
How do I stop a signal from killing my Bash script?
I believe you're looking for SIGTERM
:
Example:
#! /bin/bash
trap -- '' SIGINT SIGTERM
while true; do
date +%F_%T
sleep 1
done
Running this example cTRL+C
won't kill it nor kill <pid>
you can however kill it with kill -9 <pid>
.
If you don't want CTRL+Z
to interrupt use: trap -- '' SIGINT SIGTERM SIGTSTP
What is the difference between kill -0 and kill -9?
https://unix.stackexchange.com/a/169899/55635
kill(1)
$ man 1 kill
...
If sig is 0, then no signal is sent, but error checking is still performed.
...
kill(2)
$ man 2 kill
...
If sig is 0, then no signal is sent, but error checking is still performed;
this can be used to check for the existence of a process ID or process
group ID.
...
So signal 0 will not actually in fact send anything to your process's PID, but will in fact check whether you have permissions to do so.
While kill -9
- actually sends SIGKILL to PID
Related Topics
Package Tar.Gz into a Shell Script
Sigterm Does Not Reach Node Script When Docker Runs It with '/Bin/Sh -C'
How Does The Os Know The Real Size of The Physical Memory
Pcre Issue When Setting Up Wsgi Application
How to Manually Install The Eclipse-Cdt Plugin from an Archive/Zip on Ubuntu
Unix Domain Sockets Not Accessable Across Users
Produce Lnk File on Gnu/Linux to Transfer to Windows
Find Files Modified Over 1 Hour Ago But Less Than 3 Days
Why Having to Use Non-Blocking Fd in a Edge Triggered Epoll Function
Understanding /Boot/Config File
Autoconf Complains "C Compiler Cannot Create Executables" on Linux Mint
Any Good Tools to Solve Integer Programs on Linux
Reusing Custom Makefile for Static Library with Cmake
How to Find The Reason for a Dead Process Without Log File on Unix
Live Socket Monitoring with Netlink Inet_Diag
Why Linux Always Output "^C" Upon Pressing of Ctrl+C
How to Convert Fixed Size Dimension to Unlimited in a Netcdf File