Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Absolute BSD - The Ultimate Guide To FreeBSD (2002).pdf
Скачиваний:
25
Добавлен:
17.08.2013
Размер:
8.15 Mб
Скачать

Chapter 11: Advanced Software Management

Overview

This chapter covers several things you need to know about running software on FreeBSD.

FreeBSD can run a wide variety of software packages, most of which are available as source code so they can be built as native FreeBSD software. And, thanks to some clever design, FreeBSD can also run software from many foreign operating systems. We'll look at how to do this, focusing on the popular Linux compatibility package that allows FreeBSD to run unmodified Linux software.

Also, for your programs to start at boot, and stop cleanly when the system shuts down, you must be able to edit and write proper startup and shutdown scripts. While some programs stop just fine when you kill the operating system they're running on, others (like databases) demand a gentler shutdown process. While you can get by with a variety of ugly hacks, starting and stopping network services cleanly is an excellent habit to get into and enforce. We'll examine how to properly implement and manage these systems in FreeBSD.

And, while under normal circumstances you'll never need to know how FreeBSD's linking and shared library support works, we'll discuss how shared libraries work and how to manage and configure them. Why? Because normal circumstances are, oddly, quite rare in the computer business.

Finally, we'll look at how systems with multiple processors work, and how they interact with software. While multiple processors can greatly increase system power, they won't help you if your software isn't properly configured.

Startup and Shutdown Scripts

While FreeBSD's main system software is started by /etc/rc, add−on software is started by separate scripts. The ports and packages system installs these scripts for you. If you install your own software, however, you'll need to create a script that handles this startup and shutdown process. Plus, to change an existing add−on package's startup process, you must understand how the startup scripts function.

Note This section assumes that you have some basic understanding of shell scripts. If you've never seen or used a shell script before, read the examples here very carefully. Shell scripting is not hard, and the best way to learn is to read examples.

The /etc/rc shell scripts (see Chapter 9) handle the main system startup process. During boot up, the FreeBSD startup script checks several directories for additional shell scripts. The most popular directory for startup and shutdown scripts is /usr/local/etc/rc.d, though /usr/X11R6/etc/rc.d is another default location. These directories are specified in /etc/defaults/rc.conf, and can be overridden in /etc/rc.conf. (You can add additional script directories with the local_startup rc.conf variable.)

The shell script locator just checks in those directories for any files ending in ".sh". If it finds such a file, it assumes that the file is a shell script and executes it with an argument of start. During shutdown, FreeBSD runs these same scripts with an argument of stop. The scripts are expected to read those arguments and take appropriate action.

250

Typical Startup Script

Let's look at a typical startup script, snmpd.sh, which is part of the net−snmp package that we'll install a little later. All you need to know at this point is that it starts the SNMP server daemon at boot, and stops that same daemon on shutdown. Here's the script:

...............................................................................................

v #! /bin/sh

w if ! PREFIX=$(expr $0 : "\(/.*\)/etc/rc\.d/$(basename $0)\$"); then echo "$0: Cannot determine the PREFIX" >&2

exit 1

fi

x case "$1" in start)

[ −x ${PREFIX}/sbin/snmpd ] && ${PREFIX}/sbin/snmpd && echo −n ' snmpd'

;;

stop)

killall snmpd && echo −n ' snmpd'

;;

*)

echo "Usage: `basename $0` {start|stop}" >&2

;;

esac

exit 0

...............................................................................................

The #!/bin/sh line (v) indicates that this is a shell script. The remainder of the file (which is similar to a Windows batch file) contains commands that are run by the script.

The first section, set off by if (w) and fi, determines the path to the programs, and tells the rest of the script whether it was started in /usr/local, /usr/X11R6, or some other directory. The rest of the script

(x) needs to know this, so it can find its commands.

The ? case "$1" in line (x) is where the script actually makes a decision. This part of the script reads the first argument that the script is called with. For example, if your script is run as snmpd.sh start, start is your first argument. If you run it as snmpd.sh stop, the stop is your first argument. The script has several smaller sections: Everything between the start) and the double semicolon (;;) are steps that are taken if the first argument is start. Everything between the stop) and the next double semicolon are actions that are taken if the first argument is stop. The last option, the *), is a wildcard for all other arguments that might be typed in.

For example, if you run this script as snmpd.sh start, the script runs the following command:

...............................................................................................

[ −x ${PREFIX}/sbin/snmpd ] /&& ${PREFIX}/sbin/snmpd && echo −n ' snmpd'

...............................................................................................

These are standard UNIX shell commands. This command first checks to see if the snmpd program exists. If it does, it runs it and prints out its name. Similarly, if you call the script with a stop argument, it unceremoniously kills all snmpd processes.

251