Directories /etc/init.d and /etc/rc2.d
First, just a bit of information about how scripts located inside the /etc/init.d directory are executed.
In Linux, a runlevel is a set of commands to be executed when the system starts or at any later moment. There are
several runlevels available, each of them with its particular services to be executed. The default
runlevel in Debian (and implicitly in Ubuntu and Mint) is runlevel 2, and the corresponding directory with
scripts to be executed for this runlevel is /etc/rc2.d. Let's take a look at how this folder may look like:
[embryo@mint] /etc/rc2.d$ ls README S20dirmngr S20speech-dispatcher S23ntp S50saned S70pppd-dns S91apache2 S99ondemand S16openvpn S20kerneloops S20virtualbox-guest-utils S50rsync S70dns-clean S75sudo S99grub-common S99rc.local
Let's take a look at the README explanation:
The scripts in this directory are executed each time the system enters this runlevel. The scripts are all symbolic links whose targets are located in /etc/init.d/ . To disable a service in this runlevel, rename its script in this directory so that the new name begins with a 'K' and a two-digit number, and run 'update-rc.d script defaults' to reorder the scripts according to dependencies. A warning about the current runlevels being enabled not matching the LSB header in the init.d script will be printed. To re-enable the service, rename the script back to its original name beginning with 'S' and run update-rc.d again. For a more information see /etc/init.d/README.
Example of a Start-Up Script
Here is how a simple script with start and stop arguments may look like:
#!/bin/bash do_mount() { mount /dev/sda6 /mnt/sda6 } do_umount() { umount /dev/sda6 } case "$1" in start) do_mount ;; stop) do_umount ;; *) echo "Usage: $0 start|stop" >&2 exit 3 ;; esac
Alternatively, you can put your scripts inside the /etc/init.d folder, and then just create symbolic links inside /etc/rc2.d to your scripts. This method is actually recommended, because your scripts will be located in a single folder, and you only need symbolic links to them in any runlevel that you want to run.
#!/bin/bash # /etc/init.d/mystartup.sh # Symbolic link inside /etc/rc2.d/S99mystartup case "$1" in 'start') echo "Replace this line with your command to start the desired service." ;; 'stop') echo "Replace this line with your command to stop the desired service." ;; esac exit 0
$ ./mystartup.sh start Replace this line with your command to start the desired service. $ /floydb/tutorials/bashbg$ ./mystartup.sh stop Replace this line with your command to stop the desired service.
To see what is the current runlevel, type runlevel in the terminal. Here's a possible output:
Here are the available runlevels in a Debian system:
runlevel 0 - halt the system
runlevel 1 - single-user mode
runlevel 2 - multi-user mode (default runlevel)
runlevel 3 - multi-user mode
runlevel 4 - multi-user mode
runlevel 5 - multi-user mode
runlevel 6 - reboot the system
The default runlevel is specified in /etc/inittab. To enter a runlevel, type init N (e.g. init 2).
[embryo@mint] ~$ runlevel N 2
runlevel 0 - halt the system
runlevel 1 - single-user mode
runlevel 2 - multi-user mode (default runlevel)
runlevel 3 - multi-user mode
runlevel 4 - multi-user mode
runlevel 5 - multi-user mode
runlevel 6 - reboot the system
The default runlevel is specified in /etc/inittab. To enter a runlevel, type init N (e.g. init 2).