Terry : Run level and init scripts

Check runlevel

Print current runlevel

who -r

Output previous and current runlevel

runlevel

Examples:

  • Ubuntu default runlevel 2
  • Oracle Linux 5 & 6 default runlevel 5 (X)
  • Arch Linux default runlevel 3

SysV Style init (Debian, Ubuntu and Fedora/Red Hat, except Gentoo)

The /etc/init.d directory contains the scripts executed by init at boot time and when the init state (or "runlevel") is changed (see init(8)).

These scripts are referenced by symbolic links in the /etc/rcn.d directories. When changing runlevels, init looks in the directory /etc/rcn.d for the scripts it should execute, where n is the runlevel that is being changed to, or S for the boot-up scripts.

The names of the links all have the form Smmscript or Kmmscript where mm is a two-digit number and script is the name of the script (this should be the same as the name of the actual script in /etc/init.d).

When init changes runlevel first the targets of the links whose names start with a K are executed, each with the single argument stop, followed by the scripts prefixed with an S, each with the single argument start. (The links are those in the /etc/rcn.d directory corresponding to the new runlevel.) The K links are responsible for killing services and the S link for starting services upon entering the runlevel.

For example, if we are changing from runlevel 2 to runlevel 3, init will first execute all of the K prefixed scripts it finds in /etc/rc3.d, and then all of the S prefixed scripts in that directory. The links starting with K will cause the referred-to file to be executed with an argument of stop, and the S links with an argument of start.

The two-digit number mm is used to determine the order in which to run the scripts: low-numbered links have their scripts run first. For example, the K20 scripts will be executed before the K30 scripts. This is used when a certain service must be started before another. For example, the name server bind might need to be started before the news server inn so that inn can set up its access lists. In this case, the script that starts bind would have a lower number than the script that starts inn so that it runs first:

/etc/rc2.d/S17bind
/etc/rc2.d/S70inn

The two runlevels 0 (halt) and 6 (reboot) are slightly different. In these runlevels, the links with an S prefix are still called after those with a K prefix, but they too are called with the single argument stop.

Tools

Install and remove System-V style init script links
update-rc.d updates the System V style init script links /etc/rcrun?level.d/NNname whose target is the script /etc/init.d/name.

Debian based distribution (Ubuntu, Mint)

update-rc.d

Run-level configuration for SysV like init script links

sysv-rc-conf

Arch Linux just look into /etc/rc.d and do the manual labor...

BSD Style init (Arch Linux and Slackware)

Arch Linux
Everything is under /etc/rc.d KISS.

/etc/rc.local

/etc/rc.local is in fact triggered by /etc/init.d/rc.local script, for more information, look into its contents.

root@tux:/etc/init.d# cat /etc/init.d/rc.local 
#! /bin/sh
### BEGIN INIT INFO
# Provides:          rc.local
# Required-Start:    $remote_fs $syslog $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Run /etc/rc.local if it exist
### END INIT INFO


PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start() {
	if [ -x /etc/rc.local ]; then
	        [ "$VERBOSE" != no ] && log_begin_msg "Running local boot scripts (/etc/rc.local)"
		/etc/rc.local
		ES=$?
		[ "$VERBOSE" != no ] && log_end_msg $ES
		return $ES
	fi
}

case "$1" in
    start)
	do_start
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    stop)
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac

rc.local is the local multi-user startup script. Empty by default, it is a good place to put any last-minute commands the system should run at the very end of the boot process. Most common system configuration tasks (like loading modules, changing the console font, or setting up devices) usually have a dedicated place where they belong. To avoid confusion, ensure that whatever one intends to add to rc.local is not already residing in /etc/profile.d, or any other existing configuration location instead.

When editing this file, keep in mind that it is run after the basic setup (modules/daemons), as the root user, and whether or not X starts. Here is an example which just un-mutes the ALSA sound settings:
/etc/rc.local

#!/bin/bash
# /etc/rc.local: Local multi-user startup script.
amixer sset 'Master Mono' 50% unmute &> /dev/null
amixer sset 'Master' 50% unmute &> /dev/null
amixer sset 'PCM' 75% unmute &> /dev/null

Another common usage for rc.local is to apply various hacks when one cannot make the ordinary initialization work correctly.

Referecne

Arch Boot Process
Debian Policy Manual - System run levels and init scripts