Unix and Linux startup scripts, Part 1

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

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.

Basic inittab

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:

  • 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:

# 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.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Unix and Linux startup scripts, Part 1


2 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence







Sun Dec 6 22:35:54 2009: 7716   AndrewSmallshaw

gravatar
This has actually reminded me of an article I started writing a while back on the BSD startup mechanism which works completely differently and often appears cryptic. I don't think it actually needs much more than a polish so I'll try and get that done and send it on. I know you don't have much on BSD here but you may as well have it as someone else since I don't really want to start running a web site myself.





Sun Dec 6 23:15:53 2009: 7717   TonyLawrence

gravatar
Yes, that would be great - I'll link it into this series.



------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

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.

Contact us





As soon as an Analytical Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question will then arise — by what course of calculation can these results be arrived at by the machine in the shortest time? (Charles Babbage)

There are two major products that came out of Berkeley: LSD and UNIX. We do not believe this to be a coincidence. (Jeremy S. Anderson)








This post tagged:

BSD