How to easily add to or modify the existing subsystems of Red Hat distributions of Linux.
If you're one of those who took a chance and got one of the Caldera Previews or got a Red Hat distribution on your system, one of your original thoughts may have been the same as mine: What happened to /etc/rc.local? Where am I supposed to put my custom commands? [One answer: /etc/rc.d/rc.local is available on Red Hat systems—ED] What if I don't want the HTTP server to start?
For those of you out there who administer Sun Solaris machines, this looks quite familiar. But I was just scratching my head for a while until I wound up administering a system, and it all became clear. Time to share the knowledge.
The idea behind the setup is to make everything script-based. For each run level, scripts are run to start each individual service, instead of having a few large files to edit by hand. These scripts are located in /etc/rc.d/init.d, and most take as an option start or stop. This is to allow the specific programs to start (on bootup) or stop (on shutdown).
This setup involves a bunch of directories under /etc/rc.d/. These are:
rc0.d Contains scripts to run when the system shuts down. Technically, halt or shutdown bring the system to runlevel 0. This directory is mostly made up of kill commands.
rc1.d through rc3.d Scripts to run when the system changes runlevels. Runlevel 1 is usually single-user mode, runlevel 2 is for multi-user setup without NFS, and runlevel 3 is full multi-user and networking.
Runlevel 4 is typically unused.
rc5.d Scripts to start the system in X11 mode. This is the same as runlevel 3, with the exception that the xdm program starts, which provides a graphical login screen.
rc6.d Scripts to run when the system reboots. These scripts are called by a reboot command.
init.d Actually contains all of the scripts. The files in the rc?.d directories are really links to the scripts in the init.d directory.
Now that we know where files are located, let's look at what happens in a normal Red Hat boot sequence.
Once the system boots, /etc/rc.d/rc.sysinit is run first. The starting runlevel (specified in /etc/inittab) is found, and the /etc/rc.d/rc script is run, with the sole option being the runlevel we want to go to. For most startups, this is runlevel 3.
The rc program looks in the /etc/rc.d/rc3.d directory, executing any K* scripts (of which there are none in the rc3.d directory) with an option of stop. Then, all the S* scripts are started with an option of start. Scripts are started in numerical order—thus, the S10network script is started before the S85httpd script. This allows you to choose exactly when your script starts without having to edit files. The same is true of the K* scripts.
Let's look at what happens when we switch runlevels—say from runlevel 3 (full networking and multi-user mode) to runlevel 1 (single-user mode).
First, all the K* scripts in the level to which the system is changing are executed. My Caldera Preview II (Red Hat 2.0) setup has seven K scripts and one S script in the /etc/rc.d/rc.1/ directory. The K scripts shut down nfs, sendmail, lpd, inet, cron, and syslog. The S script then kills off any remaining programs and executes init -t1 S, which tells the system to really go into single-user mode.
Once in single-user mode, you can switch back to full multi-user mode by typing init 3.
There are two additional points I can make here.
First, you can selectively start and stop scripts, even those not native to your runlevel. Executing scripts in /etc/rc.d/init.d/ with an option of start or stop will start up or stop the programs or services which the script controls. This allows you to turn off NFS from runlevel 3 while keeping all other systems active, for example. Similarly, you can start NFS back up when you are ready.
Stopping NFS in this case would require stopping two systems—nfsfs and nfs. The nfsfs script will mount or ummount any of the NFS-mounted file-systems listed in your /etc/fstab. The nfs script would then shut down the processes associated with NFS, in this case mountd and nfsd.
So the proper procedure for shutting down NFS would be:
# /etc/rc.d/init.d/nfsfs stop Unmounting remote filesystems. # /etc/rc.d/init.d/nfs stop Shutting down NFS services: rpc.mountd rpc.nfsd #
And starting NFS would be:
# /etc/rc.d/init.d/nfs start Starting NFS services: rpc.mountd rpc.nfsd # /etc/rc.d/init.d/nfsfs start Mounting remote filesystems. #
Do you want to not start the HTTP daemon, without removing the file from the rc3.d directory? Easy. Rename /etc/rc.d/rc3.d/S85httpd to something that does not start with a capital S or a capital K. Your best bet would be to rename files using a lowercase “s” or a lowercase “k”. This way, not only will the scripts not be started, but they'll appear later in an ls file listing, since entries starting with capital letters are shown separately from those beginning with lower case letters. So you'd wind up with a file now called s85httpd, which is somewhat separated from the rest of the entries an an ls -l listing.
An important note here, though: make sure you know what the scripts are doing when you disable them. If you disable something like the S10network script, none of your networking software will work. This is why S10network has such a low number: because other scripts are dependent on the network and must be executed after the network software is in place.
You want to make your own init processes to start and stop? That's easy enough to do. Make a script that accepts the word start as an option. Not all scripts need to be able to stop, but if the script starts a process in the background, you should almost certainly include a stop option as well. For example, a script that polls the time over the network every time you enter runlevel 3 does not need a stop. A script that starts a program to query the network time every 15 minutes would need a start and a stop script, since the program the script started is continuously running. A program for the second example is better suited from a crontab, but being able to do things your own way is at the heart of Unix, isn't it?
Once it's written (and tested), put it in the /etc/rc.d/init.d/ directory. Let's say it's the program to check the time on a network machine every 15 minutes, so we'll call the script “netdate”. Once it is in the /etc/rc.d/init.d/ directory, you can make links in the directories you want to start it in. If you want your program to run in runlevel 3, make a link to your script from /etc/rc.d/rc3.d/S??netdate. Make ?? a number that will fit in the rest of the directory, such as S55netdate. You'll want it to be above S10 so that the network is started, and S55 isn't taken, so it seems a good enough location. It's not required that there be only one script with each number, but it is good form, since you know exactly what order the scripts will be started in.
If you want to stop the process gracefully during a shutdown, make sure your script accepts stop, then make a link to /etc/rc.d/init.d/netdate from /etc/rc.d/rc0.d/K55netdate. Again, you should make sure the number you use is not being used by another subsystem to avoid confusion.
You can test your new setup by using init 3. Since the other subsystems are already running, the only one that will start is the one you just added. If the init 3 command hangs, your script didn't exit; you probably need to put an ampersand at the end of a line to put the problem process in the background. You can also run your script manually from the /etc/rc.d/init.d/ directory.
Now that you know how the subsystems work, you can easily add or modify the existing subsystems for your particular Linux setup.