Hone logo
Hone
Problems

Graceful Shutdown with Signal Handling

Signal handling is crucial for building robust and responsive applications, especially those that need to shut down cleanly in response to external events or system signals. This challenge asks you to implement a Python program that gracefully handles the SIGINT (Ctrl+C) and SIGTERM signals, performing a cleanup action before exiting. This is vital for preventing data loss and ensuring a smooth transition when your application is interrupted.

Problem Description

You need to create a Python script that registers signal handlers for SIGINT (typically triggered by Ctrl+C) and SIGTERM (typically sent by system termination commands like kill). When either of these signals is received, the script should:

  1. Print a message indicating that a shutdown signal has been received.
  2. Perform a cleanup action: Print a message indicating that cleanup is in progress.
  3. Exit with a status code of 0, indicating successful termination.

The script should continue to run normally until a signal is received. The cleanup action should be simple (e.g., printing a message), but in a real-world application, it could involve closing files, releasing resources, or saving data.

Examples

Example 1:

Input: User presses Ctrl+C while the script is running.
Output:
Signal received: SIGINT
Cleaning up...

Explanation: The script receives SIGINT, prints the signal message, then prints the cleanup message before exiting.

Example 2:

Input: User sends a SIGTERM signal to the script (e.g., using `kill -15 <pid>`).
Output:
Signal received: SIGTERM
Cleaning up...

Explanation: The script receives SIGTERM, prints the signal message, then prints the cleanup message before exiting.

Example 3: (Edge Case - No Signal)

Input: The script runs indefinitely without receiving any signals.
Output: (No output until a signal is received)

Explanation: The script continues to run until a signal is received. No output is generated if no signal is sent.

Constraints

  • The script must handle both SIGINT and SIGTERM signals.
  • The signal handlers must be registered using the signal module.
  • The cleanup action must be performed before the script exits.
  • The script must exit with a status code of 0 upon receiving either signal.
  • The script should not block indefinitely if a signal is not received. It should be able to run and wait for a signal.

Notes

  • The signal module provides functions for registering signal handlers. Look into signal.signal() and signal.SIGINT and signal.SIGTERM.
  • Consider using a global variable or a flag to indicate that the script should exit after the cleanup action is complete. This is important to prevent the script from continuing to execute code after the signal handler has been invoked.
  • The cleanup action can be as simple as printing a message, but think about what a real-world application might need to do during shutdown.
  • On Windows, signal handling is less reliable than on Unix-like systems. This challenge is primarily intended for Unix-like environments. While you can attempt to implement signal handling on Windows, the behavior might not be as predictable.
Loading editor...
python