Sobes.tech
Middle

What are signals? Which ones do you know? What is the difference between SIGTERM and SIGKILL?

sobes.tech AI

Answer from AI

Signals are mechanisms in Unix-like systems for asynchronous notification of processes about events (such as interruption, termination, errors). They allow managing processes and responding to system events.

Main signals:

  • SIGINT — interruption (usually Ctrl+C)
  • SIGTERM — request for graceful process termination
  • SIGKILL — immediate process termination without possibility of handling
  • SIGHUP — hangup signal, often used for reloading configuration

Difference between SIGTERM and SIGKILL:

  • SIGTERM (15) — a friendly request to terminate the process. The process can catch this signal, perform cleanup, and terminate properly.
  • SIGKILL (9) — forceful process termination that cannot be caught or ignored. The process terminates immediately without cleanup.

Example of handling SIGTERM in Python:

import signal
import sys

def handler(signum, frame):
    print('Received SIGTERM, terminating...')
    sys.exit(0)

signal.signal(signal.SIGTERM, handler)

# Long loop
while True:
    pass