Linux - init scripts

mail

How to create an init script ?

This whole article is obsoleted by systemd and systemctl.

Here's a template for init (source) :

#!/bin/bash
######################################### myInitScript.sh ###########################################
# chkconfig: 2345 20 80
# description: Description comes here....
#
# version: 20160609
########################################## ##########################################################

UNIX_SUCCESS=0
UNIX_FAILURE=1

# Source function library.
. /etc/init.d/functions		maybe not a good idea 

start() {
	# code to start app comes here
	# example: daemon program_name &
	}

stop() {
	# code to stop app comes here
	# example: killproc program_name
	}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		stop
		start
		;;
	status)
		# code to check status of app comes here
		# example: status program_name
		;;
	*)
		echo "Usage: $0 {start|stop|status|restart}"
esac

exit $UNIX_SUCCESS

When the system runlevel changes (increase/decrease runlevel, boot and shutdown), init scripts (aka startup scripts) are executed with a single argument being one of (source) :

mail

How to register an init script ?

This whole article is obsoleted by systemd and systemctl.

The Debian method :

  • to register a script or a daemon : update-rc.d slapd defaults
  • to unregister it : update-rc.d -f slapd remove

Details (sources : 1, 2) :

update-rc.d myCommand defaults 100
myCommand
The command / script / binary to be executed
defaults
Synonymous for "start in runlevels 0123 and stop in 456". Runlevels can be specified explicitly : ... start runlevelStart stop runlevelStop
100
While entering / leaving each runlevel, processes are started / stopped in the order given by these numbers.

The new Red Hat method

  • to register a script or a daemon : chkconfig slapd on
  • to unregister it : chkconfig slapd off

The old Red Hat method

  1. find the default runlevel : grep initdefault /etc/inittab
    Which may output : id:2:initdefault:, meaning 2 is the default runlevel. This could be 3, or rarely 4 or 5.
  2. Create a rc.local file (as root):
    touch /etc/init.d/rc.local; chmod 774 /etc/init.d/rc.local
  3. Set it to be run at boot time :
    ln -s /etc/init.d/rc.local /etc/rcn.d/S99local
    Replace the n with the default runlevel from step one. For example : rc2.d
  4. Edit the /etc/init.d/rc.local script.

End Notes

This'll cause the rc.local script to be run last during the bootup process (because of the 99). This is generally what you want to do, to make sure your network connection is up and all the basic services are started before custom init script runs.

If for some reason you have commands you need to run sometime earlier in the bootup process, you can create multiple scripts this way. It doesn't matter what you name them, just stick them in /etc/init.d, then symlink to them from /etc/rcX.d. Make sure the name of the symlink starts with a capital S, and is followed by a two-digit number : the lower the number, the sooner during bootup the script will be run. Don't run it too early or your filesystems might not even be mounted yet !

If you want a script to ALWAYS be run, no matter what runlevel you boot into, even in Single User Mode (runlevel 1), make a symlink in /etc/rcS.d instead of /etc/rcn.d.