delayed-shutdown is an initscript that delays the shutdown (runlevel 0) or reboot (runlevel 6) procedure as long as a pre-defined lock file exists. The goal is to create a mechanism, which can be used by programs that perform critical operations that must not be interrupted, in order to delay system shutdown until these programs have finished their job. The whole concept is very simple: the program, for instance a backup script, creates a pre-defined lockfile at a pre-defined location (lock is acquired). If a shutdown or reboot is initiated while the script is still active, delayed-shutdown checks for the existence of the lockfile and, if it finds it, it delays the system shutdown/reboot while it periodically continues to check for the existence of the lockfile. Whenever the backup script finishes its operation and deletes the lockfile (lock is released), delayed-shutdown lets the system go down.
Note: in a previous post I had tried to implement a shutdown/reboot/suspend prevention switch using various inefficient methods. After receiving some pointers from members of the Linux-Greek-Users mailing list, I studied the initscript mechanism and wrote this solution. It has been tested on Fedora only and is released for Fedora and RedHat based linux distributions. I would appreciate feedback about its compatibility with Debian or Gentoo based systems.
Manual Installation
As user ‘root‘ follow the instructions to install the file in your initscripts directory:
cp delayed-shutdown /etc/init.d/ chmod +x /etc/init.d/delayed-shutdown
For RedHat and relatives run:
chkconfig --add delayed-shutdown
For Debian and relatives run:
update-rc.d delayed-shutdown defaults
Manual De-installation
If for any reason you need to remove ‘delayed-shutdown’ from your system do the following:
For RedHat and relatives run:
chkconfig --del delayed-shutdown
For Debian and relatives run:
update-rc.d -f delayed-shutdown remove
Delete the initscript:
rm /etc/init.d/delayed-shutdown
Usage
In order to delay the shutdown process until your software finishes its operation, you should create the no-shutdown lock file:
/var/lock/noshutdown.lock
Then let your software do its job and, when finished, delete the no-shutdown lock file. In case of a script that would be:
lockfile /var/lock/noshutdown.lock ... [script is working] ... rm -f /var/lock/noshutdown.lock
Test
A test script, test.sh, is included in the distribution package. In order perform a test, do the following:
- Make sure you have installed the delayed-shutdown initscript as described in the “installation section.
- Run test.sh: ./test.sh &
- Reboot or shutdown the machine
Test.sh
will acquire the lock, sleep in the background for 90 seconds and then release the lock. The shutdown/reboot procedure will continue after the lock has been released.
Download
All versions of the software, including the latest stable release, are available from the development web site’s download area.
Misc
The initscript delayed-shutdown is released under the terms of the MIT license.
delayed-shutdown initscript by George Notaras is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2008 - Some Rights Reserved
thanks for sharing and documenting it so well!
i have put it on github in case someone wants to work on it (i plan to at least :)
https://github.com/stefanct/delayed-shutdown
one additional idea i have is to use a directory of lock files not only a single lock file. this would allow different processes to create lock files safely without one process removing the lock file of another one and creating chaos. i have not looked at your solution in detail, but i think you did not handle that possibility yet.
Hi,
I’m glad you found this little script useful. This is an interesting feature to add. Please feel free to improve it as you wish. However, for completeness, I’d suggest you forked the initial repo located here:
https://github.com/gnotaras/delayed-shutdown
I just uploaded it.
Also, be advised that this script might be really dangerous in case the job, which is performed while this script blocks shutdown, fails to complete. In that case the box would be inaccessible and a reset would be required.