SIGKILL signal Handler
You cannot, at least not for the process being killed.
What you can do is arrange for the parent process to watch for the child process's death, and act accordingly. Any decent process supervision system, such as daemontools, has such a facility built in.
Handle a kill signal
This is it: you cannot catch a SIGKILL
, it will definitely kill your program, this is what it's been created for.
SIGKILL signal handling
Signal are "handed off" to a process by the kernel, so sending a signal from processA to processB employs the kernel. When SIGKILL is delivered the kernel does not allow any activity by the process (user mode), specifically process rundown: atexit calls, _exit. Nothing. The process is simply destroyed by the system. This involves some activity in kernel mode. Buffered data is lost. SYSV semaphores and other kernel persistent memory objects are left in memory. It can be a real mess.
If something in kernel memory is causing a hang you use the sysrq interface in linux:
http://tldp.org/HOWTO/Remote-Serial-Console-HOWTO/security-sysrq.html
--to perform whatever semblance of an ordered shutdown you can get.
This is why using SIGKILL is an absolute last resort, because you cannot know what you are breaking. And it will not fix all hangs.
What exactly are you working on?
Could linux signal SIG_KILL be handled by process sighandler?
No. As Matteo Piano pointed out, we're not supposed to be able handle SIGKILL using a signal handler. There shouldn't be a difference in this regard between different kernel versions unless we're talking about a buggy version or an otherwise non-standard version.
From the "Standard signals" section of man 7 signal:
The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
If one were determined enough though, one could download the Linux kernel sources and change it to do whatever they'd like. But I wouldn't suggest allowing SIGKILL to be caught and I wouldn't call it "safe" if one did. You could wind up having to do hardware resets to get processes to stop.
As you don't specify in your question why you'd want to catch SIGKILL,
you may want to consider using another signal like SIGHUP
, SIGINT
or SIGQUIT
instead. These other signals are meant for possibly being handled and some Unix/Linux programs do just that.
Alternatively (to catching SIGKILL), you may also want to consider having a process sit around to detect whether the target process exits. A parent process for instance could look for a SIGCHLD
signal from the child exiting and then effectively cause the same events to occur that you had hoped to do in your signal handler.
Signal handling in Uvicorn with FastAPI
FastAPI allows defining event handlers (functions) that need to be executed before the application starts up, or when the application is shutting down. Thus, you could use the shutdown
event, as described here:
@app.on_event("shutdown")
def shutdown_event():
# close connections here
Related Topics
What Are Libtool's .La File For
Setting Default Permissions for Newly Created Files and Sub-Directories Under a Directory in Linux
How to Create a Configure Script
Linux: Compute a Single Hash for a Given Folder & Contents
Unit Testing for Shell Scripts
Apache Not Accepting Incoming Connections from Outside of Localhost
How to Recall the Argument of the Previous Bash Command
How to Read the Source Code of Shell Commands
Linux Equivalent of the MAC Os X "Open" Command
How to Have the Cp Command Create Any Necessary Folders for Copying a File to a Destination
Best Practice to Run Linux Service as a Different User
Command to Change the Default Home Directory of a User
Relinking an Anonymous (Unlinked But Open) File
Linking 32-Bit Library to 64-Bit Program
How to Run a Windows Executable from Wsl (Ubuntu) Bash
Restarting Cron After Changing Crontab File
Linux Capabilities (Setcap) Seems to Disable Ld_Library_Path