I wrote Automating Program Startup almost 10 years ago. A lot has changed since then and I realized that this has actually become a fairly complicated subject when I tried to answer a forum post that simply asked about starting an arbitrary service. Because that poster used "httpd" as an example and was using a Fedora system, several answers mentioned "chkconfig". That's the right answer if we are specifically asking about httpd on a Fedora system, but it's not the right answer for any arbitrary service on any arbitrary Unix or Linux system.
In the beginning, there was "init". If you had a Unix system, you had "init" and it was almost certainly process id 1. Process 0 was the swapper (not visible in "ps"); it started init with a fork and, one way or another, init was responsible for starting everything else.
There were variations, but not many. BSD systems simply used /etc/rc and /etc/ttys. You'd configure terminals in /etc/ttys and add anything else to /etc/rc. The System V Unixes used the more complex /etc/inittab file to direct init's actions, but really that wasn't much more than the /etc/ttys - life was pretty simple. That didn't take long to change.
The Automating Program Startup article describes init and inittab as they worked on SCO Unix and Linux at that time. Linux inittab soon became mostly standardized, though with some confusion of /etc/init.d vs. /etc/rc.d/init.d and Red Hat's use of "chkconfig" (or the GUI "ntsysv" or "serviceconf" ) to control the startup scripts. With any Unix or Linux system that uses inittab, you simply need to start with /etc/inittab and follow the trail from there. For example, a recent Centos system has these entries in /etc/inittab
l0:0:wait:/etc/rc.d/rc 0 l1:1:wait:/etc/rc.d/rc 1 l2:2:wait:/etc/rc.d/rc 2 l3:3:wait:/etc/rc.d/rc 3 l4:4:wait:/etc/rc.d/rc 4 l5:5:wait:/etc/rc.d/rc 5 l6:6:wait:/etc/rc.d/rc 6
From that, you know (if you read the older article) that when the system enters a particular run state, it will be running the /etc/rc script and passing the run state as an argument. The /etc/rc script will take different actions based on what run state is desired; you can examine the script to see just what it does and how it does it.
A recent SCO system uses separate scripts for each run level:
r0:056:wait:/etc/rc0 1> /dev/console 2>&1 </dev/console r1:1:wait:/etc/rc1 1> /dev/console 2>&1 </dev/console r2:2:wait:/etc/rc2 1> /dev/console 2>&1 </dev/console r3:3:wait:/etc/rc3 1> /dev/console 2>&1 </dev/console sd:0:wait:/etc/uadmin 2 0 >/dev/console 2>&1 </dev/console fw:5:wait:/etc/uadmin 2 2 >/dev/console 2>&1 </dev/console rb:6:wait:/etc/uadmin 2 1 >/dev/console 2>&1 </dev/console
Again. you'd examine the individual scripts to see what they do, but in general, on Linux or any other system, you'll find these scripts look for "S" or "K" scripts in a particular directory and take appropriate action (see the older article for more detail on those and variants like SCO's "P" scripts).
"S" scripts are run when the system enters a particular run level; "K" scripts run when they leave it.
Note that on Centos, /etc/rc3.d is a link to /etc/rc.d/rc3.d. Other systems may not have these links but will follow the same concepts.
But now we start to get a little more complicated. Let's say that you want a particular process to start. We'll use "httpd" as an example because it is something that is commonly found on Unix systems and you may or may not want it running. What makes it run? We'll look at the SCO and Centos systems as examples.
SCO's default run-level is 2. Most Linux systems use 3 for normal multiuser mode and 5 if X11 is desired. You can find out where the system is now with "who -r"; a SCO system would indicate "2" normally and a Linux box would show "3" or "5". Since SCO defaults to "2" and inittab tells init to run /etc/rc2 for that run-level, we look at that script and find that it in turns runs through scripts found in /etc/rc2.d. One of those scripts is P90apache. If it's not there, you'd type "apache enable" to create it. If it is there and you don't want it to start, you could do one of four things:
Note that you could get more clever than "exit 0". You could test for the existence of some file and continue the startup if it exists or exit if not.
For Centos, we'll look in /etc/rc5.d and will probably find either K15httpd or S85httpd (the numbers may be different on your system). Both of these are links to /etc/rc.d/init.d/httpd. Centos uses Red Hat's "chkconfig" to enable or disable common startup scripts:
# pwd /etc/rc5.d # ls *httpd K15httpd # chkconfig httpd on # ls *httpd S85httpd # chkconfig httpd off # ls *httpd K15httpd #
However, just as you can on SCO, you could control this manually by renaming, editing or removing scripts (and on systems that don't use chkconfig, you may have to).
What if you want to add your own script? On SCO, you'd create an appropriate "S" script in /etc/rc2.d and a matching "K" script in /etc/rc0.d. For a Linux system, you could do the same thing (though in /etc/rc.d/rc5.d or /etc/rc.d/rc3.d) or you could let chkconfig manage all that for you. All you need is a "chkconfig" comment line (in your script) that might look like this:
# chkconfig: - 91 17
Look at any of the existing scripts to see examples. The "91" says what will follow the "S" when chkconfig links a startup script to this; the "17" is used for "K" scripts. You put your script in /etc/rc.d/init.d and activate it:
# chkconfig --add mystuff # chkconfig --list mystuff mystuff 0:off 1:off 2:off 3:off 4:off 5:off 6:off # chkconfig mystuff on # chkconfig --list mystuff mystuff 0:off 1:off 2:on 3:on 4:on 5:on 6:off
If you examine the rc[3-4] directories, you'll see your command has been added:
# ls /etc/rc.d/rc[3-4].d/*mystuff /etc/rc.d/rc3.d/S91mystuff /etc/rc.d/rc4.d/S91mystuff
That's enough for Part 1. In Part 2 we'll let Andrew Smallshaw show us how BSD startup scripts work today.
Got something to add? Send me email.
More Articles by Anthony Lawrence © 2011-03-29 Anthony Lawrence