Init Scripts
From Zanecorpwiki
Contents |
Overview
Initialization scripts are (in Linux Standard Base compliant distros) are found in /etc/init.d. These scripts are run by the boot routines to initialize the system and standard services. This approach allows administrators to modify and select startup services.
Calling
Within the /etc/init.d directory, there are 8 sub-directories: rc0.d, rc1.d, rc2.d, rc3.d, rc4.d, rc5.d, rc6.d, and rcS.d. These directories correspond to the various run-levels. The most important are 3 and 5. Level 3 is "full boot" with multi-user support. Level 5 is full boot with graphical login.
The names of the content of these directories should all start with either 'S' or 'K', which stand for "start" and "kill" (TODO: I think). Following the S and K is a two digit number, left zero padded, so the first scripts are '01'[notes 1] are run first and the last scripts are the '99'. The S scripts are run, in order, on startup and the K scripts on shutdown.[notes 2]
Script Specification
Modes
Each script is called with a single argument that tells the script what action to take. The most important are 'start' and 'stop'[1]:
- start : Starts the service.
- stop : Stops the service.
- restart : Stops and restarts the service if it is already running. Otherwise, starts the service.
- try-restart : Restarts the service only if it was active before.[notes 3]
- reload : (optional) Causes the configuration of the service to be reloaded without actually stopping and restarting the service.
- force-reload : Causes the configuration to be reloaded if the service supports this. Otherwise, the service is restarted.
- status : Prints the current status of the service.
Exit Codes for Non-Status Modes[1]
- 0 : success
- 1 : generic or unspecified error
- 2 : invalid or excess arguments
- 3 : unimplemented feature (e.g., "reload")
- 4 : user had insufficient privileges
- 5 : program is not installed
- 6 : program is not configured
- 7 : program is not running
- 8-99 : reserved for future LSB use
- 100-149 : reserved for distribution use
- 150-199 : reserved for application use
- 200-254 : reserved
Exit Codes for Status Modes[1]
- 0 : program is running or service is OK
- 1 : program is dead and /var/run pid file exists
- 2 : program is dead and /var/lock lock file exists
- 3 : program is not running
- 4 : program or service status is unknown
- 5-99 : reserved for future LSB use
- 100-149 : reserved for distribution use
- 150-199 : reserved for application use
- 200-254 : reserved
Skeleton Script
TOOD: reference the /etc/init.d and the SuSE reference.
Notes
- ↑ I assume '00' is possible as well, but it's common to start with 01.
- ↑ Technically, the scripts are run when the system steps up or down to or from a run-level, but 99.9% of the time, we never deal with scenarios other than startup and shutdown.
- ↑ Defined in LSB 1.9, some versions of Red Hat compliant to earlier versions call this 'condrestart'.


