22.1.3  服务器启动脚本

用于启动服务器应用程序(更确切地说是服务器守护进程)的脚本全部位于/etc/init.d目录下,每个脚本控制一个特定的守护进程(这个概念将在22.2.1节具体介绍)。所有的脚本都应该认识start和stop参数,分别表示启动和停止服务器守护进程。下面这条命令启动了SSH服务器的守护进程。

 

$ sudo /etc/init.d/sshd start

Starting SSH daemon                                                   done

 

与此相对的,下面这条命令停止SSH服务器的守护进程。

 

$ sudo /etc/init.d/sshd stop

Shutting down SSH daemon                                              done

大部分启动脚本还认识restart参数。顾名思义,接收到这个参数的脚本首先关闭服务器守护进程,然后再启动它。

 

$ sudo /etc/init.d/sshd restart

Shutting down SSH daemon                                              done

Starting SSH daemon                                                    done

 

在改变运行级(包括系统启动和关闭)的时候,系统执行的是rclevel.d目录下的脚本文件。仍然以SSH为例,使用ls -l命令可以清楚地看到init.d和rclevel.d这两个目录下脚本文件之间的关系。

 

$ ls -l /etc/init.d/rc5.d/ | grep sshd

lrwxrwxrwx 1 root root  7 11-09 17:55 K12sshd -> ../sshd

lrwxrwxrwx 1 root root  7 11-09 17:55 S10sshd -> ../sshd

 

/etc/init.d/rc5.d目录下的两个脚本文件K12sshd和S10sshd,实际上都是指向/etc/init.d/sshd的符号链接。init在执行脚本的时候,会给以字母S开头的脚本文件传递start参数,而给以字母K开头的脚本文件传递stop参数。例如init运行K12sshd时,实际执行的是下面这条命令。

 

/etc/init.d/rc5.d/K12sshd stop

 

由于K12sshd脚本是/etc/init.d/sshd的符号链接,因此又等价于下面这条命令。

 

/etc/init.d/sshd stop

 

脚本文件名中的数字描述了脚本运行的先后顺序,数字较小的脚本首先被执行。下面的例子反映了这一点。当进入运行级5的时候,S05network在S10sshd之前执行(因为5<10);类似地,当退出运行级5的时候,K12sshd在K17network之前执行(因为12<17)。

 

$ ls -l /etc/init.d/rc5.d/ | egrep 'ssh|network'

lrwxrwxrwx 1 root root  7 11-09 17:55 K12sshd -> ../sshd

lrwxrwxrwx 1 root root 10 11-09 17:50 K17network -> ../network

lrwxrwxrwx 1 root root 10 11-09 17:50 S05network -> ../network

lrwxrwxrwx 1 root root  7 11-09 17:55 S10sshd -> ../sshd

 

这样安排的用意很明显,供远程登录使用的SSH服务器不应该在网络接口启动之前运行。在向rclevel.d目录下手动添加脚本的时候应该格外注意这些依赖关系。下面列出了在笔者的openSUSE系统上启动服务器脚本的顺序。

 

$ ls -l /etc/init.d/rc5.d/

...

lrwxrwxrwx 1 root root    8    11-09 17:50 S01acpid -> ../acpid

lrwxrwxrwx 1 root root    7    11-09 17:50 S01dbus -> ../dbus

lrwxrwxrwx 1 root root    14    11-09 17:50 S01earlysyslog -> ../earlysyslog

lrwxrwxrwx 1 root root    8    11-09 17:50 S01fbset -> ../fbset

lrwxrwxrwx  1 root root    16    11-09 17:50 S01microcode.ctl -> ../microcode.
ctl

lrwxrwxrwx 1 root root    9    11-09 17:50 S01random -> ../random

lrwxrwxrwx 1 root root    9    11-09 17:50 S01resmgr -> ../resmgr

lrwxrwxrwx 1 root root    21    11-09 18:10  S01SuSEfirewall2_init -> ../SuSEf-
irewall2_init

lrwxrwxrwx 1 root root    13    11-09 17:50 S02consolekit -> ../consolekit

lrwxrwxrwx 1 root root    12    11-09 17:50 S03haldaemon -> ../haldaemon

lrwxrwxrwx 1 root root    11    11-09 17:50 S04earlyxdm -> ../earlyxdm

lrwxrwxrwx 1 root root    10    11-09 17:50 S05network -> ../network

lrwxrwxrwx 1 root root    9    11-09 17:50 S06syslog -> ../syslog

lrwxrwxrwx 1 root root    9    11-09 17:55 S07auditd -> ../auditd

lrwxrwxrwx 1 root root    10    11-09 17:55 S07portmap -> ../portmap

lrwxrwxrwx 1 root root    8    11-27 13:06 S07smbfs -> ../smbfs

lrwxrwxrwx 1 root root    15    11-09 17:55 S07splash_early -> ../splash_early

lrwxrwxrwx 1 root root    12    11-09 17:55 S10alsasound -> ../alsasound

lrwxrwxrwx 1 root root    15    11-09 17:55 S10avahi-daemon -> ../avahi-daemon

lrwxrwxrwx 1 root root    7    11-09 17:55 S10cups -> ../cups

lrwxrwxrwx 1 root root    19    11-09 17:55 S10java.binfmt_misc -> ../java.
binfmt_misc

lrwxrwxrwx 1 root root    6    11-09 17:55 S10kbd -> ../kbd

lrwxrwxrwx 1 root root    7    11-09 17:55 S10nscd -> ../nscd

lrwxrwxrwx 1 root root    13    11-09 17:56 S10powersaved -> ../powersaved

lrwxrwxrwx 1 root root    9    11-09 17:55 S10splash -> ../splash

lrwxrwxrwx 1 root root    7    11-09 17:55 S10sshd -> ../sshd

lrwxrwxrwx 1 root root    15    11-09 17:56 S10vmware-guest -> ../vmware-guest

lrwxrwxrwx 1 root root    17    11-09 17:55 S11avahi-dnsconfd -> ../avahi-
dnsconfd

lrwxrwxrwx 1 root root    12    12-21 05:25 S11nfsserver -> ../nfsserver

lrwxrwxrwx 1 root root    10    11-09 17:55 S11postfix -> ../postfix

lrwxrwxrwx 1 root root    6    11-09 17:55 S11xdm -> ../xdm

lrwxrwxrwx 1 root root    7    11-09 17:55 S12cron -> ../cron

lrwxrwxrwx 1 root root    9    11-09 17:57 S12smartd -> ../smartd

lrwxrwxrwx 1 root root    9    11-09 14:15 S12xinetd -> ../xinetd

lrwxrwxrwx 1 root root    15    11-09 17:50 S21stopblktrace -> ../stopblktrace

lrwxrwxrwx 1 root root    22    11-09 18:10 S21SuSEfirewall2_setup -> ../S-
uSEfirewall2_setup