Creating New SysV Startup Scripts

If you add a new server or other program that should launch whenever you start the computer, the easy way to get it to run is usually to add an entry to a local startup script, as described earlier in "Using Local Startup Scripts." SysV startup scripts exist for several reasons, though, and some of these apply even to locally compiled programs. Most importantly, SysV startup scripts enable you to configure a server to run in some runlevels but not others, and to start up and shut down using relatively simple commands—by passing the strings start or stop to the script.

The core idea behind a SysV startup script is fairly straightforward: It's a script to start, stop, and sometimes restart or check the status of a program. Most Linux distributions, though, add a lot of extras to their SysV startup scripts. These extra features include a summary of the startup status for programs as they're launched when the system boots, storage of process IDs (PIDs) in /var/run, and so on. Unfortunately, distributions have different methods of implementing these extra features. If you want to use similar features in your own SysV startup script, you may want to create yours by starting from an existing script. You can then replace calls to the original server with calls to your own. You should peruse several scripts to locate one that's best-suited to your modifications.

If you're willing to settle for a simpler SysV startup script, something like Listing 9.1 may fit the bill. This script starts and stops the fictitious bigserv program. Its method of stopping the server is to use killall, which kills all instances of the server. Some servers include provisions to store their PIDs in files. You can use this feature to store a PID when you start a server, and to kill only that instance when you shut it down, if you like.

Listing 9.1 : Sample SysV Startup Script

/usr/local/bin/bigserv

stop)

/usr/bin/killall bigserv

restart) $0 stop $0 start

Once you've created a SysV startup script, you should store it in the SysV startup script directory for your distribution, such as /etc/rc.d/init.d for Mandrake or Red Hat. If you want the server to start or stop automatically when you boot or change runlevels, you must then create links to the original file in the runlevel-specific SysV startup script directories, such as /etc/rc.d/rc?.d for Mandrake and Red Hat. These links' names should start with S if you want the server to start or K if you want the server to stop in that runlevel. Filenames also conventionally include a two-digit sequence number. For instance, to start the bigserv server, you might have a startup script link called S67bigserv and a shutdown link called K23bigserv in another runlevel directory. The runlevel management tools automatically pass a start parameter to files whose names begin with S and stop to those whose names begin with K. The sequence number determines the order in which the scripts run. For some packages, this isn't terribly important; but for others it is. For instance, most servers should start after the network or networking script runs, and they should be shut down before that script is run for shutting down the network in runlevels 0, 1, and 6. Each distribution uses its own set of sequence numbers, so you should study these sequences before inserting a new script into the sequence.

Note Chapter 22, "Running Servers," provides more information on SysV startup scripts. Chapter 22 also covers tools that can help you manage these scripts by helping you enable and disable SysV startup scripts.

0 0

Post a comment