Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to catch any OS shut down and execute some actions before letting the OS be shutted down. I'm using Fedora 19 and C++.

I'm able to catch a shutdown signal given by the console with the following C++ code :

myAct.sa_handler = myStructure::mySignalHandler;
sigaction(mySignal, &act, NULL);

The mySignal signal can be activated via /etc/init.d and a custom code executed before a shutdown is made.

On Fedora 12 Kernel 2.6, this solutions works perfectly. The solution works on Fedora 19 Kernel 3.9.5 if I try to shut down the computer using the graphic interface.

However, I can't manage to execute my code when a command is used in a terminal to reboot or shut down the computer on Fedora 19 kernel 3.9.5 The solution doesn't work anymore since it dropped the usual init.d script system.

Putting my script on the /usr/lib/systemsd location doesn't work, the script is executed, but the other scripts are also executed. Thus, the systems shuts down not treating my commands.

How may I solve that problem?

share|improve this question
A shutdown doesn't send a “shutdown signal”: there's no such thing. Usually shutdown sends SIGTERM, waits a little, then sends SIGKILL. What signal are you catching? –  Gilles Apr 23 '14 at 21:33
I want to execute code before the SIGTERM signal is sent by the OS. –  Xaltar Apr 24 '14 at 7:47
Then you would not be getting a signal. You would need to run your code from the shutdown code invoked by systemd. I think this requires being root (it does with sysvinit and upstart, and probably does with systemd as well since otherwise a user could block a shutdown). –  Gilles Apr 24 '14 at 10:24
That's precisely what I'm trying to do ... –  Xaltar Apr 24 '14 at 11:13

1 Answer 1

The signal can be caught from a running process. So, if you start a long-living process (aka daemon) on boot-up, then you will indeed receive a SIGTERM from that process and do whatever you want from the signal handler. However, this is the worst method possible: an extra process doing nothing and just waiting for a signal to arrive.

You have better ways to achieve this, esp. with systemd. The unit file (foo.service) will look as following:

Description=Perform my actions


However, keep in mind that due to parallel nature of systemd this unit can be started arbitrarily late in the shutdown process (although this can be influenced by careful use of dependencies).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.