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
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.
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
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.
(Article continues after the break)
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:
- Remove the script from /etc/rc2.d with "rm"
- run "apache disable" to remove the script
- Rename the script so that it does not start with "S", "K" or "P"
- Edit the script to add "exit 0" to the top
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:
# ls *httpd
# chkconfig httpd on
# ls *httpd
# chkconfig httpd off
# ls *httpd
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
That's enough for Part 1. In Part 2 we'll let Andrew Smallshaw show us how BSD startup scripts work today.
If this page was useful to you, please help others find it:
More Articles by Anthony Lawrence
- Find me on Google+
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Jump to Comments
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.