How to start a program or script at boot time

at the right moment and with root privileges

1. Getting ready for the show.

This tutorial shows a simple way to get your programs or scripts running at boot time.
Let's imagine we want a set of programs and/or scripts to start at boot.
First we prepare the programs and the scripts.
We will call the programs from scripts so we assume from now on that we only have scripts.
We assume the name of the scripts are of the kind: and so on.
Those scripts will be located at the folder /usr/local/sbin.

For instance, imagine that is a script that calls a program with several options.
Then the content of the script should be something like this:

# I am a very basic script calling a program with some options

/usr/bin/myprogram3 -option1 -option2 -option3

Notice that we wrote the path where the program is located.

2. Preparing the simplest /etc/init.d script

The programs or services that are started at boot time have an script in the folder /etc/init.d/. Many of the scripts already present in that directory will give you an example of the kind of things that you can do. Here's is the simplest script which is divided into two parts, code which runs when called with "start", or code that stops running when called with "stop".

#! /bin/sh
# Provides: myprogram
# Should-Start: console-screen dbus network-manager
# Required-Start: $remote_fs $network $local_fs
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start myprogram at boot time

set -e

. /lib/lsb/init-functions


case "$1" in
     skill $PROGRAMNAME

exit 0

To use this example just do the following:

Once you have your script you give it a name like myprogram, give to it execution permission (chmod +x myprogram), and copy it to the folder /etc/init.d as root. Then you check that the script works by calling it as root:

sudo /etc/init.d/myprogram start:

and you check that it also stops:

sudo /etc/init.d/myprogram stop

During boot the script myprogram will be called in this way. When you stop the computer, it will also stopped in this way. If skill does not its job, the OS will stop it anyway (which is a dirty way but it is simple right?).

3. Boot order

Now we have to tell the OS that we want our program to be be started at boot time.
There are several services and programs that are called initiated at boot time.
In our script we have a line such as # Required-Start: $remote_fs $network $local_fs.
This means that our script will be executed after the system has mounted all the filesystems and the network facility is on service. If you want/need your script to be executed at the end of the boot, change the line # Required-Start: like this

# Required-Start: $all
The last step follows. Open a terminal and type the following:
sudo insserv myprogram

This way we ask the OS to use our script at boot time at the right moment. If we check the folder /etc/rc2.d we will see that a file of the type SXmyprogram has been added. The number X indicates the order in which every program is called.

4. References

This is a quick and a bit dirty way (but effective) of starting a program at boot time. If you want to know a bit more, check the following:

and also it is good to read the files located at /etc/init.d/README and /etc/rc2.d/README and an example of init script: /etc/init.d/skeleton. Good luck!

Raconet Linux