0

I want to detect that system is restarting before it terminates my program on Linux. I tried using /var/run/utmp file to detect runlevel, put inotify on its changes but seems like system is closing this program before I get signal. I catch shutdown with it if I set runlevel with telinit command, but dont catch if I just restart with button on top-right corner in Ubuntu.

Any idea how can it be done?

Zhani Baramidze
  • 1,407
  • 1
  • 13
  • 32
  • 2
    Normally the restarting thing would send your process a `SIGTERM`. Why can't you catch it? See [signal(7)](http://man7.org/linux/man-pages/man7/signal.7.html) – Basile Starynkevitch Dec 01 '14 at 11:24
  • As a program running on the box is it your business to know why you are being shutdown, don't you just want to save state if you have it and exit in either case? – Matthew V Carey Dec 01 '14 at 11:29

1 Answers1

3

Catch the SIGTERM signal and be quick with saving/doing whatever and then exit. You've got approximately 10 seconds before you'll get SIGKILL which you can't catch, and you'll be force terminated.

If the system isn't sending you a SIGTERM to allow proper shutdown, change your system to something proper, this is the standard way of doing it.

See man 7 signal and man 3 sigaction for signal handling.

(Note that I don't know of a standard way to check if a system is rebooting or not, I don't think such thing exists. But as mentioned above, a proper system will send you SIGTERM and let you do your cleanup/exit. Hard reboot excluded, because thats almost equivalent of pulling the power cord.)

Jite
  • 4,250
  • 1
  • 17
  • 18
  • you mean catch sigterm and then check runlevel? – Zhani Baramidze Dec 01 '14 at 11:25
  • @JaniBaramidze I don't know of a standard way to check if the system is rebooting. I don't have runlevels on my system since I run systemd for instance. By catching `SIGTERM` you solve the reboot-problem and introduce proper shutdown when, for instance, a process manager wants to shut you down for any other reason too. – Jite Dec 01 '14 at 11:27
  • @JaniBaramidze Why do you need to separate a "before reboot" shutdown and a "regular shutdown"? – Jite Dec 01 '14 at 11:38
  • what do you mean? I just need to write to some file if system is rebooting/shutting down before it kills my program – Zhani Baramidze Dec 01 '14 at 11:43
  • @JaniBaramidze A process can be told to shutdown no matter if your system is rebooting or not. So with your first comment above here, what I wonder is why you need to check runlevel after receiving a SIGTERM? When you type reboot in console for instance, your program shall get a SIGTERM signal from your init daemon (usually). SIGTERM is used to tell the process/program that it should save it stuff/cleanup and then exit. What I wonder is if reboot-part is important? – Jite Dec 01 '14 at 11:50
  • my program starts on system startup, so I want to know if it was running before last shutdown or not – Zhani Baramidze Dec 01 '14 at 11:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65952/discussion-between-jite-and-jani-baramidze). – Jite Dec 01 '14 at 11:53