Python Detect Ubuntu Shutdown Signal

Oct 28, 2020
import signalimport timeis_shutdown = Falsedef stop(signum, frame):    global is_shutdown    is_shutdown = True    print('SIGTERM')signal.signal(signal.SIGTERM, stop)print('START')while not is_shutdown:    print('.', end='', flush=True)    time.sleep(1)print('END')
  • SIGINT: Interrupt from keyboard (CTRL + C).
  • SIGTERM: Termination signal.
  • SIGHUP: Hangup detected on controlling terminal or death of controlling process.
  • SIGKILL: Kill signal. It cannot be caught, blocked, or ignored.

NOTE: For systemd stop, you will receive SIGTERM followed by SIGHUP.

Reference:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.