Using init.d Scripts to Execute Commands at Start-Up

Posted by Chris7mas on Jan 18, 2014 1:26 AM EDT
TuxArena; By Craciun Dan
Mail this story
Print this story

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.

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:

#!/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

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.

#!/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

You can test your script by running it in Bash with the start and stop parameters. For example, mystartup.sh start to execute the commands inside the start block and mystartup.sh stop to execute the commands inside the stop block:

$ ./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:

[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).

Full Story

  Nav
» Read more about: Story Type: Tutorial; Groups: Debian

« Return to the newswire homepage

This topic does not have any threads posted yet!

You cannot post until you login.