Startup Scripts


When a Unix system boots, it launches several startup scripts to initialize the system services. Note that this section applies mostly to Fedora Core and FreeBSD.

To find the startup scripts

1.

cd /etc

System-configuration files live in the /etc directory, and the startup scripts are no exception to this rule.

Code listing 2.1. Fedora Core 3's rc files.
 [root@dhcppc2 root]# cd /etc [root@dhcppc2 etc]# ls -F | grep rc bashrc csh.cshrc imrc inputrc mail.rc Muttrc pinforc rc@ rc0.d@ rc1.d@ rc2.d@ rc3.d@ rc4.d@ rc5.d@ rc6.d@ rc.d/ rc.local@ rc.sysinit@ slrn.rc vimrc wgetrc 

2.

ls -lF | grep rc

List all of the files and directories, and search for the ones that have rc in them. This turns up a few extra files, but you'll be able to see the various rc files, directories, and (in the case of Linux) symbolic links.

As you can see, Fedora Core 3 (Code Listing 2.1), FreeBSD 5.3 (Code Listing 2.2), and Mac OS X .(Code Listing 2.3) all have slightly different rc files available.

Now that you know where to find these files, you might want to know how to add your own startup commands to the system boot process.

Code listing 2.2. FreeBSD's rc files.
 bsd# cd /etc bsd# ls -F | grep rc csh.cshrc locate.rc mail.rc rc rc.conf rc.d/ rc.firewall rc.firewall6 rc.resume* rc.sendmail rc.shutdown rc.subr rc.suspend* 

Code listing 2.3. Mac OS X's rc files.
 bender:~ chrish$ cd /etc bender:/etc chrish$ ls -F | grep rc bashrc csh.cshrc efax.rc mail.rc rc rc.boot rc.cleanup rc.common rc.netboot 

Code listing 2.4. A skeleton startup script.
 #!/bin/sh # # FreeBSD keywords used to control when # this startup script is started on # a FreeBSD system (these are ignored # on other OSes): # # PROVIDE: insert-name-here # REQUIRE: list-prerequisite-services # BEFORE: launch-before-this # KEYWORD: FreeBSD # Include OS-specific code. # # You might want to read these scripts # to see what standard startup script # functions are provided for you. case "$(uname -s)" in     Linux)         # Linux         . /etc/init.d/functions         ;;     FreeBSD)         # FreeBSD         . /etc/rc.subr         ;;     CYGWIN*)         # Cygwin         ;;     Darwin)         # Mac OS X         . /etc/rc.common         ;; esac # Handle the command-line options. case "$1" in     start)         # INSERT START HANDLING HERE         ;;     stop)         # INSERT STOP HANDLING HERE         ;;     restart)         # INSERT RESTART HANDLING HERE         ;;     *)         # Ignore other commands.  Note         # that FreeBSD also supports         # "status", "poll", and "rcvar"         # commands.  See the rc manpage         # for more information.         exit 0         ;; esac 

To add startup commands

1.

Create your startup script using your favorite text editor. Startup scripts must respond appropriately to start, stop, and restart arguments. Code Listing 2.4 shows you a skeleton script that you can start with; just add your own code at the INSERT points.

If you're using FreeBSD, you need to add several comments that indicate the name of the service your script is providing (the PROVIDE comment in Code Listing 2.4), which services are required before your script can start (the REQUIRE list in Code Listing 2.4), the service that your script needs to start before (this is usually login in the BEFORE comment, Code Listing 2.4), and the FreeBSD KEYWORD line (Code Listing 2.4).

If you're using Mac OS X, you'll also need to create a StartupParameters.plist file (Code Listing 2.5). Fill in the short-description and service-name with a description of your script and its name. The prerequisites are a list of other service names in quotes; if you don't require anything specific, use Resolver. The order-value can be Early, Late, or None.

2.

chmod +x scriptname

Be sure to set the executable bit on your script, or it won't run.

Remember to test your script, too!

3.

cd /etc/rc.d

Or, if you're using Fedora Core Linux (other Linux distributions may differ slightly; there are no standards for different run levels, so check the documentation):

cd /etc/rc.d/rc5.d

Or, if you're using Mac OS X:

cd /Library/StartupItems

Obviously you'll need to be in the same directory as the startup scripts to add your own command to the launch sequence.

4.

cp scriptname.

Copy your new startup script to the current directory.

If you're using Linux, the name of the script will control when it starts. The scripts in the various rc.d directories are started in lexicographical order, so you'll need to rename your script with the mv command to fit it into the existing sequence.

If you're using Mac OS X, each startup script goes into its own directory (with a name that matches your script) with the StartupParameters.plist file.

Code listing 2.5. A sample StartupParameters.plist file for Mac OS X.
 {     Description = "short-description";     Provides = ("service-name");     Requires = ("prerequisites");     OrderPreference = "order-value";     Messages =     {         start = "Starting service-name";         stop = "Stopping service-name";         restart = "Restarting service-name";     }; } 

Tips

  • To follow the standard form for startup scripts in Fedora Core, copy your script into /etc/init.d and then create a symbolic link to the script in the appropriate /etc/rc.d directory. For example, if you're logged in as root, you'd create 999myscript:

     cp 999myscript /etc/init.d ln -s /etc/init.d/999myscript /etc/rc5.d/999myscript 

  • To add a Unix daemon as a Windows service, use the cygrunsrv program. For example, you could use this command to install a Service that starts the Cygwin inetd (Internet daemon) every time the system boots:

     cygrunsrv -I inetd -d "Cygwin inetd" -p /usr/sbin/inetd -a -d -e CYGWIN=ntsec 

    This installs (-I) a service named inetd with a description (-d) of "Cygwin inetd". The service launches (-p) /usr/sbin/inetd with the -d argument (-a) and the CYGWIN environment variable (-e) set to ntsec.

    We'll look at specific uses for cygrunsrv later in the book, but you can also run it with help to see a help message.

  • If your Mac OS X system doesn't already have a /Library/StartupItems directory, you can create one and give it the correct permissions with these commands:

     mkdir -p /Library/StartupItems chmod 0755 /Library/StartupItems sudo chown root:admin /Library/ StartupItems 

  • Scripts run from /Library/StartupItems on Mac OS X will have root permissions unless the script calls sudo to run its commands as another user.

  • Windows systems use their own, mostly hidden startup procedures. It's better to let Windows start itself normally, and add Cygwin daemons as "native" services using the cygrunsrv command.

  • Although Mac OS X systems have /etc/rc files (but not an /etc/rc.d directory because of its BSD heritage), their startup process is handled differently from that used by standard Unix systems. We'll show you how to start services on Mac OS X without disrupting the normal boot process. If you want to know more about Mac OS X's boot process, take a look at "SystemStarter and the Mac OS X Startup Process" (www.opendarwin.org/~kevin/SystemStarter/SystemStarter.html).




    Unix Advanced. Visual QuickPro Guide
    Unix Advanced: Visual QuickPro Guide
    ISBN: 0321205499
    EAN: 2147483647
    Year: 2003
    Pages: 116

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net