1

I know that you can detect SIGHUP, SIGTERM, SIGQUIT etc, but is it possible to detect when the system receives a halt / poweroff / shutdown signal ?

crankshaft
  • 2,607
  • 4
  • 45
  • 77

2 Answers2

2

Signals like SIGHUP, SIGTERM, SIGQUIT are sent to a specific process and can be handled there. Powerr off and shutdown are handled by the init process of your system. They depend on the implementation of init you are using (Upstart, SysV init), and there is no general way to detect and handle them from another process, regardless of whether this process is written in Python or any other language.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
1

All processes are sent the SIGTERM signal, and then the SIGKILL signal. To my knowledge, there is no way to know if those signals are sent specifically because of a shutdown or not. See this relevant question for more information.

Community
  • 1
  • 1
Paul Mougel
  • 16,728
  • 6
  • 57
  • 64