1

I wanted to catch a SIGHUP signal in my Python program. But I ran into the problem that Python doesn't recognize the signal name:

import signal
import time

def handler(sig, frame):
  print("SIGNAL:", sig)


signal (SIGHUP, handler)

while True:
  time.sleep(1)

Python does not recognize SIGHUP and gives an error. Anyone knows how to fix it?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Kek_9_11
  • 29
  • 3

1 Answers1

3

In python you have to explicitly import the signals themselves. This should work:

from signal import signal, SIGHUP
import time 

def handler(sig, frame):
  print("SIGNAL:", sig) 

signal (SIGHUP, handler) 

while True: 
  time.sleep(1) 
user9102437
  • 600
  • 1
  • 10
  • 24