4. Changing what a runlevel does

There are Debian ways of munging the directory of scripts that the /etc/init.d/rc script calls upon.

4.1. Create a script to start or stop a feature

In order to affect a runlevel, you need to create a script that does some work.

# cd /etc/init.d
# cp skeleton my_sample_script

That skeleton is a nice way to start you off on the right foot. Once you get your my_sample_script working, you can add it to various runlevels.

4.2. Determine where it belongs in the START or STOP sequence

Even when your script works properly, you'll need to know where it belongs in the startup sequence   for example, to check out runlevel 4:

# cd /etc/rc4.d
$ ls -F
S10sysklogd@  S20gpm@      S20postgresql@  S50wu-ftpd@  S99rmnologin@
S12kerneld@   S20inetd@    S20ssh@         S60sshd@
S19bind@      S20logoutd@  S20xinetd@      S89atd@
S20cipe@      S20makedev@  S22ntpdate@     S89cron@
S20exim@      S20mysql@    S23ntp@         S91apache@

This sample, taken from my system, shows there are zero KILL scripts, and 21 START scripts. If you wanted your new script to be STARTed after sshd (which is number 60) and before atd (number 89) then using any number near S70 for startup would do the trick.

Let's say you also want to run the KILL portion (maybe to refresh or disable something, it's up to you) when entering runlevel 4, as well. In my example above, I have NO kill scripts, so there's no established sequence to fit into so any number will do, but let's use 50.

4.3. Install the links that call the script

Here's how you set up your script to START as sequence #70 and STOP as sequence #50 for runlevel 4:

# update-rc.d my_sample_script start 70 4 . stop 50 4 .

That'll create links in /etc/rc4.d/ called K50my_sample_script and S70my_sample_script that point to your original script. The actual script itself is in /etc/init.d/my_sample_script.

You could also add your start/stop script to several runlevels at once. For example, add it to runlevels 2 through 5 like this:

# update-rc.d my_sample_script start 70 2 3 4 5 . stop 50 2 3 4 5 .

You'll wind up with this:

Note

I'll do this later

Easy as pie!

4.4. Removing a feature from a runlevel

When you decide to REMOVE your script (or any other service) from a runlevel, do like so:

# cd /etc/init.d
# mv my_sample_script my_sample_script.DISABLED
# update-rc.d my_sample_script remove

If you don't remove (or rename, if you're anal like I am and deplore actually deleting anything, ever) the actual script first, you'll need to supply other arguments to update-rc.d to get rid of the links.

Of course you can always tangle things up manually if you like, by editing /etc/init.d/* scripts and munging links yourself... but that way, lies madness.