3

So based on my understanding of the Unix/Linux shutdown process (which is highly limited I admit), I think at some point these steps happen: 1. mounted drives get unmounted 2. a termination signal gets sent to running applications, which allows those applications to do clean-up before exiting

Now, which happens first? Or does #1 not happen at all? Or is the order not guaranteed?

The reason I ask this question is that I am working on an application which is writing data to a central configuration file on shutdown, where the central configuration file can be placed anywhere. My concern is that if the central configuration file is on a mounted drive, and #1 happens before #2, then the stored path I have for the central configuration file may no longer be valid. Does this concern make sense?

2
  • 1
    If it unmounted before sending the signal, the processes wouldn't be able to clean up.
    – Barmar
    Nov 9, 2014 at 22:12
  • They wouldn't even be able to run, since the program code comes from mounted disks.
    – Barmar
    Nov 9, 2014 at 22:13

1 Answer 1

2

This is dependent on the init system and on how it's configured. But any sane configuration allows system software to register a hook to run on shutdown. With a traditional System V init, that's a file in /etc/rc0.d. With a traditional BSD init, that's code in /etc/rc.shutdown. With Upstart, that's a runlevel change event to 0 or 6. The list goes on, which is why maintaining packages for all the distributions out there can be a lot of work (most authors would provide a shutdown script for their application, and let a distribution maintainer make a package that registers the shutdown script in the right place).

Once all the application-specific scripts have executed, the final phases of the shutdown take place. Typically, this involves unmounting all filesystems except the ones where there are still open files, then killing all remaining processes, and finally unmounting the remaining filesystems (except the root which is remounted read-only) and halting or rebooting the computer. It's common to send all processes a TERM signal, which is catchable, then a few seconds later send a KILL signal. While a process can try to react, there is limited time and the system won't wait.

So if your application is installed as root, you simply need to register a shutdown-time init script. If your application is installed by unprivileged users, you can't have the guarantee that you'll be able to do anything during the shutdown phase.

Note that you need to gracefully handle a system crash or power failure anyway. Shutdown-time actions should be limited to things that make the next boot more efficient, there shouldn't be anything that absolutely needs to be done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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