This tutorial will show you how to automatically execute commands at the system start-up using the standard init.d directory and the default runlevel. These scripts are called init scripts or start-up scripts, they are used to start and stop services and they will be executed with root privileges.
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
These are actually symbolic links to scripts located inside the /etc/init.d directory. All the scripts here that start with the S character will be started, being executed with the start argument. To prevent a service from starting up, you would change the name of the script so it begins with a K, followed by a number which is the difference between 100 and its start-up order. For example, let's take the S91apache2 script. To prevent it from starting up we would rename it as K09apache2. The digits following the S character represent the order in which these scripts will be executed.

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.
It's here in this directory where we will put all the scripts we want executed with root privileges at start-up. The order in which they are executed depends on the number provided after the S character. I recommend using S99 as a prefix for your personal scripts, so they will be executed the last, after all other services have started.
Example of a Start-Up Script
Here is how a simple script with start and stop arguments may look like:

do_mount() {
  mount /dev/sda6 /mnt/sda6

do_umount() {
  umount /dev/sda6

case "$1" in
    echo "Usage: $0 start|stop" >&2
    exit 3
Now you can replace the mount /dev/sda6 /mnt/sda6 line inside the do_mount() function with any commands that you want to be automatically executed. Note that this is just an example, you would normally mount and unmount partitions using /etc/fstab and /etc/mtab.

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.


# /etc/init.d/
# Symbolic link inside /etc/rc2.d/S99mystartup

case "$1" in
    echo "Replace this line with your command to start the desired service."
    echo "Replace this line with your command to stop the desired service."

exit 0
You can test your script by running it in Bash with the start and stop parameters. For example, start to execute the commands inside the start block and stop to execute the commands inside the stop block:
$ ./ start
Replace this line with your command to start the desired service.
$ /floydb/tutorials/bashbg$ ./ 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:
[embryo@mint] ~$ runlevel 
N 2
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).
By Craciun Dan on January 10, 2014 | Updated: January 17, 2014 | v0.2.0 r1
TuxArena Projects
Online Readers