System Initialization and Services
◾
465
You can also control the services through the command line in one of two ways. First,
you can use the
service
program, located in /sbin. The
format of this command is
/sbin/service
service command
where
service
is the service in question, and command is one of
start
,
stop
,
restart
,
or
status
. The start and stop commands should be obvious, starting or stopping a given
service. The restart command first stops the service and then starts it. You would use restart
after you have modified the service’s configuration file. Status provides you the status of the
service, whether it is running or stopped.
You can also control the service through the service control script. The service control
scripts are all stored in /
etc/init.d
,
for instance, /
etc/init.d/nfs
. You can directly
provide the
command to the service as in
/etc/init.d/nfs
command
where the command again is one of
start
,
stop
,
restart
, or
status
. If your current
working directory is /etc/init.d, then you can issue the command as
./nfs
command
11.5.4 Examining the atd Service Control Script
The start/stop commands are not actually issued to the service itself but instead to the
script that controls the service. These scripts are stored in /etc/init.d with symbolic links
from the various /etc/rc#.d directories to them. To get a better understanding of these
scripts, we examine the /etc/init.d/atd script,
*
which controls atd, the at daemon. Below, we
see this script file. The script’s code is interspersed among comments that describe what
the script is doing.
#!/bin/sh
#
# atd Starts/stop the “at” daemon
#
# chkconfig: 345 95 5
# description: Runs commands scheduled by the “at” command at the time \
# specified when “at” was run, and runs batch commands when the load \
# average is low enough.
###
BEGIN INIT INFO
# Provides: atd at batch
# Required-Start: $local_fs
*
The atd script as well as the atd service, the at program, and all of Linux is protected under the GNU General Public
License, v3.0. For more information, see http://www.gnu.org/copyleft/gpl.html. The script is being reprinted here with-
out permission as per part 4 of the GPL, which guarantees the right to provide “verbatim copies” of course code as long
as the copyright notice is conspicuously and appropriately published.
466
◾
Linux with Operating System Concepts
# Required-Stop: $local_fs
# Default-Start: 345
# Default-Stop: 95
# Short-Description: Starts/stop the “at” daemon
# Description: Runs commands scheduled by the “at”
command at the time
# specified when “at” was run, and runs batch commands when the load
# average is low enough.
### END INIT INFO
The above lines of the script are all comments explaining information about the script.
The line chkconfig provides the default enable information, that is, the levels in which
atd is automatically started: 3, 4, and 5. The next number, 95, is atd’s start priority. This
number corresponds to the two-digit value in any symbolic link name starting with an S
that points to atd. For instance, in /etc/rc5.d, the symbolic link is named S95atd. The last
digit, 5, is the stop priority that corresponds to the two-digit value in any symbolic link
name starting with a K that points to atd. So, for instance, /etc/rc1.d has the symbolic link
K05atd. The provided line indicates which programs rely on this service. As we see, atd, at,
and batch all rely on this atd script to start the atd service. Required-Start and Required-
Stop list services that rely on this service. In this case, $local_fs is a variable. Default-Start
and Default-Stop are as explained earlier in the paragraph.
# Source function library.
./etc/rc.d/init.d/functions
The first executable line of atd executes the script /etc/rc.d/init.d/functions. This file con-
tains a variety of commands and function definitions that may be used by the scripts in the
/etc/init.d. Stepping through the functions file, we see the following actions:
TEXTDOMAIN
=
initscripts
umask 022
PATH
=
“/sbin:/usr/sbin:/bin:/usr/bin”
export PATH
In these first four instructions, we see the assignment of two environment variables and
a umask statement. The environment variable PATH is set up so
that the various scripts in
this directory can reference instructions without full paths. Similarly, if a file has to be cre-
ated, it will be given initial permissions of 644 (666–022). We will see in atd that it creates a
file known as
lockfile
, used to determine whether the atd service is running or stopped.
Both the umask statement and the creation of PATH are used so that the atd service has
values assigned to it in spite of root already having a umask value and PATH variable.
As the script file function continues to execute, it defines a number of additional environ-
ment variables, including CONSOLETYPE, COLUMNS, and SETCOLOR_NORMAL, all
used to properly display in a terminal window or on console. Several if-then and if-then-
else statements are used to define further environment variables.
The script then defines a
System Initialization and Services
◾
467
number of functions that will be used by the various script files such as atd. For instance, the
function checkpid will be used to determine if a PID is already assigned to a process, which
is useful to see if the process is currently running or not. The function pidofproc obtains the
PID of a running process. The function killproc is used to stop a running process.
Another function of note is pids_var_run. This function is used by status (explained
below) to test to see if a given process has both a PID and a pid file. The pid file is stored
under /var/run to indicate that a particular process has started running. The user must
have sufficient access rights to view the pid file or else this function terminates with a
return value of 4 to indicate insufficient privilege to access the file. If there is a pid file but
no PID for this program, then the function returns 1 indicating
that the program is dead
even though a pid file exists. If there is a PID and pid file, the function returns 0 indicating
the program is running; otherwise, the function returns 3 indicating that the program is
not running. The function daemon is used to start an actual daemon service.
Let us now focus on the
status
function from the functions file. We will refer to the sta-
tus function every time we issue the command /
sbin/service
Do'stlaringiz bilan baham: