«
»

delayed-shutdown initscript

November 28th, 2008 by George Notaras

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.

The delayed-shutdown initscript by George Notaras, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Terms and conditions beyond the scope of this license may be available at www.g-loaded.eu.

Related Articles

Tags: , , ,

Bookmark and Share

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped=""> <pre>