Here’s a nice explanation of the (Debian) boot process (from the Debian Administration)
There are a lot of misconceptions voiced here about the sysvinit system. It is complex because it HAS to be. To quote Eistein, “Make things as simple as possible, and no simpler.”
Sysvinit is the best way known to manage all the complexity involved in making any combination of the 10,000+ Debian packages load the various daemons they need, in the order they need, only to the extent they are installed, and have them all work smoothly together. The older BSD init system was simpler, but less capable– the init scripts had to be edited for every package you added that needed to start at boot time, and the order was always a potential source of a problem. That is why most Linux distributions use sysvinit– it has proven its value over many years.
Each package that needs to load at a certain runlevel puts one script in /etc/init.d, usually named with the package name. This allows you to start, stop, restart, reload, etc. it by typing “/etc/init.d/mypackage start” (or stop or reload or whatever). If you write a script, use others as a template, and change what you need to change. Test the script by typing these commands manually (probably at a root shell). If the script works properly here, it should work in runlevels. However, to answer the last plea for help, this script does NOT start anything automatically at ANY runlevel. We still need one more thing for that.
To do that, we create symlinks to these scripts in the various runlevel directories. For example, if we normally run at runlevel 2 (as Debian does by default) and we want mypackage to run as start, then create two links in /etc/rc2.d, one named S99mypackage and one named K99mypackage. S means Start, and K means Kill (stop) the process. When the system goes into runlevel 2, all the start links in /etc/rc2.d will be run one after another. The 99 tells the system WHEN it should be run– they run in numerical order from 01-99 (and in alpha order within those numeric levels, I think). So 99 will be run last, and everything should basically already be up by then, usually a good choice for your personal stuff. But if you want it run before thing1 and after thing2, then pick a more appropriate number. If you want it started first and stopped last, then name the links S01mypackage and K99mypackage. You can also disable the link temporarily by renaming the uppercase S or K to lowercase– only the uppercase letters will be run.
As to “why so many runlevels, if Debian only uses two?”– you can use them any way you want. Runlevel S (or 1) is single-user maintenance mode (which may not even mount your disks). Runlevel 2 (in Debian) is multi-user. Some distributions once used runlevel 3 for networking, or 5 for X windows. Now both (networking and X) seem to be nearly universal, and are not usually put on separate runlevels. But if you want to do that, you can. It’s your system, do what you want. Maybe you have a system needs to be configured differently at home and work, or when the kids are using it. You can set runlevels for these different modes.
If you want the simpler “just run this at boot time” of BSD, just add your script to the /etc/rc.local script which runs after the rest of the boot process is complete. So you CAN have it both ways.
Hope this clears up some of the confusion. I’d say RTFM, but sometimes the manuals are long on detail and short on 30,000 foot-level explanation of the concepts.