There’re lots of ways to start up a program or script when a Linux system boots up. Not all of them work for all types of applications. This article covers several methods you can configure your program to start up automatically at system boot up. It’s based on Ubuntu Linux, but I think they should apply to many other Linux distributions too.
Method 1: Add your Program in the System Initialization
When a Linux system is powered on, it will first run the program in BIOS. The BIOS locates the boot device and then the Linux kernel starts to boot up.
The kernel will mount the root file system, and run /sbin/init program, which normally has a process ID of 1. You can use ps -ax command to check.
The /sbin/init program will run script /etc/init.d/rcS, which in turn calls all S??* scripts in /etc/rcS.d/ directory in numerical/alphabetical order. Using ls -l command shows the content of my Ubuntu system as below,
-rw-r--r-- 1 root root 447 2009-09-08 02:58 README lrwxrwxrwx 1 root root 24 2010-11-24 18:16 S06keyboard-setup -> ../init.d/keyboard-setup lrwxrwxrwx 1 root root 21 2010-11-24 18:16 S13pcmciautils -> ../init.d/pcmciautils lrwxrwxrwx 1 root root 16 2010-11-24 18:16 S25brltty -> ../init.d/brltty lrwxrwxrwx 1 root root 18 2010-11-24 18:16 S37apparmor -> ../init.d/apparmor lrwxrwxrwx 1 root root 23 2010-11-24 18:16 S49console-setup -> ../init.d/console-setup lrwxrwxrwx 1 root root 17 2010-11-24 18:16 S55urandom -> ../init.d/urandom lrwxrwxrwx 1 root root 21 2010-12-02 10:47 S65firestarter -> ../init.d/firestarter lrwxrwxrwx 1 root root 24 2010-11-24 18:16 S70screen-cleanup -> ../init.d/screen-cleanup lrwxrwxrwx 1 root root 20 2010-11-24 18:16 S70x11-common -> ../init.d/x11-common lrwxrwxrwx 1 root root 19 2011-04-05 10:50 S75policykit -> ../init.d/policykit
After rcS, the system will switch to default run level. The default run level is defined in /etc/inittab. If not found, it will refer to /etc/init/rc-sysinit.conf file.
Run levels refers to a mode of operation, it’s used to configure a selected group of process to run. There’re 7 of them, from 0 to 6. A very brief summary is given below,
1 single user
2-4 user defined
Once the default run level is found, the initialization will execute all the scripts under /etc/rc*.d, where * is the default run level.
In my Ubuntu system, there is no /etc/inittab file and the default level is set to 2 in /etc/init/rc-sysinit.conf file, so the initialization will execute the scripts under /etc/rc2.d/ directory. Use ls -l shows the following in my /etc/rc2.d/ directory,
lrwxrwxrwx 1 root root 13 2011-02-21 15:45 K50ntp -> ../init.d/ntp -rw-r--r-- 1 root root 677 2009-11-10 17:44 README lrwxrwxrwx 1 root root 15 2011-03-01 18:09 S00kdump -> ../init.d/kdump lrwxrwxrwx 1 root root 18 2011-03-11 12:36 S10sysklogd -> ../init.d/sysklogd lrwxrwxrwx 1 root root 15 2011-03-11 12:36 S11klogd -> ../init.d/klogd lrwxrwxrwx 1 root root 14 2011-04-28 10:23 S12dbus -> ../init.d/dbus …... lrwxrwxrwx 1 root root 18 2010-11-24 18:16 S99ondemand -> ../init.d/ondemand lrwxrwxrwx 1 root root 18 2010-11-24 18:16 S99rc.local -> ../init.d/rc.local
Note that there’s README file. It actually contains quite useful information. It’s worth reading it if you want to change something in this directory.
Also note that a the end of the list, there is rc.local, which is execute at the end of rc*.d directory.
The /etc/init.d/rc.local script will run all the programs/scripts in the /etc/rc.local script.
All the above actions are carried out in the boot up process and before a specific user is logged in. So if you want to start up your program using super user privilege, you can add your program here. The step-by-step instruction is as below,
1. put scripts/programs to /etc/init.d/
sudo cp /home/my_script /etc/init.d/
2. sudo chown root scriptname.sh
3. sudo chmod 4755 scriptname.sh
4. sudo update-rc.d scriptname.sh defaults