Thursday, 19 June 2008

How to start a script at boot time

Making script start at the boot time is really not a big deal, it's done with ease with a few commands and actually no knowledge about system runlevels is required for simple tasks. But a little background info won't hurt.Runlevel is a state or mode in which system works, and each runlevel uses its own specific services/scripts. Usually there are 8 of them:

* 0 System Halt
* 1 Single user
* 2 Full multi-user mode (Default)
* 3-5 Same as 2
* 6 System Reboot

There is also runlevel called "s" which is used by the system when it changes runlevels.
Default runlevel for a Linux based system is 2 generally. Finding out in which runlevel system resides is done running 'runlevel' command as root
$ sudo runlevel
[sudo] password for tvrtko:
N 2

Number 2 means system is in runlevel 2 and the N character means 'None' - system was in that runlevel since boot.
For each runlevel there is a directory filled with needed scripts in /etc. On Debian/Ubuntu systems these directories are called: rc0.d, rc1.d, rc2.d, rc3.d, rc4.d, rc5.d and rc6.d, and if you open one of them you'll see all those scripts with weird names that are actually just links to real scripts that reside in /etc/init.d directory. Command update-rc.d makes those links, and I will explain its most basic usage.

Suppose I have a script named "myfirewall" filled with lots of commands needed to configure my network and protection, and I need to start it every time the system boots. This script must reside in /etc/init.d directory for this to work, and it must be executable.
$ chmod 755 /etc/init.d/myfirewall

All that it must be now is to run update-rc.d command like this:
$ sudo update-rc.d myfirewall defaults
password:
Adding system startup for /etc/init.d/myfirewall ...
/etc/rc0.d/K20myfirewall -> ../init.d/myfirewall
/etc/rc1.d/K20myfirewall -> ../init.d/myfirewall
/etc/rc6.d/K20myfirewall -> ../init.d/myfirewall
/etc/rc2.d/S20myfirewall -> ../init.d/myfirewall
/etc/rc3.d/S20myfirewall -> ../init.d/myfirewall
/etc/rc4.d/S20myfirewall -> ../init.d/myfirewall
/etc/rc5.d/S20myfirewall -> ../init.d/myfirewall

And that's it. The script will run for every runlevel now. Notice the script will run as root user. If you need to run some service as a less privileged user simply create a script in /etc/init.d with the line:
su [username_you_want_as_owner] -c /path/to/program

To remove script from startup sequence:
$ update-rc.d -f  myfirewall remove

This should be enough for a start.

No comments: