rc.local不执行,不生效(rc.d rc.local rc.sysinit 等系统脚本启动顺序)
http://topic.csdn.net/u/20101127/21/870a0645-958d-4223-b155-68bdf2b8e6c0.html
rc.local文件 本身的文件是在/etc/rc.d/rc.local
/etc/rc.local是一个到/etc/rc.d/rc.local的软连接
/etc/rc3.d/S99local
/etc/rc5.d/S99local
这2个又分别是到/etc/rc.local的软连接
你一会确认一下
/etc/rc3.d/S99local
/etc/rc5.d/S99local
里 是不是有你添加的内容 如果没有 肯定就是连接被破坏了
http://www.jsxubar.info/linux-rc-d-rc-local-rc-sysinit.html
本文详细解析了 Linux系统 rc.d rc.local rc.sysinit 等系统脚本启动顺序,让你在使用这些脚本时能得心应手。

Linux系统有多个命令如service, setup , chkconfig 控制系统服务和启动脚本的设置。
一、Redhat 系统启动顺序
Redhat 系统按如下系统启动:
- 加载内核
- 执行init程序
- /etc/rc.d/rc.sysinit # 由init执行的第一个脚本
- /etc/rc.d/rc${RUNLEVEL}d/* # $RUNLEVEL为缺省的运行模式
- /etc/rc.d/rc.local #相应级别服务启动之后、在执行该文件(其实也可以把需要执行的命令写到该文件中)
- /sbin/mingetty # 等待用户登录
其中 /etc/rc.d/rc.sysinit 执行与运行模式(即runlevel,包括1,2,3,4,5,6六个级别)无关的相同的初始化任务:
- 调入keymap以及系统字体
- 启动swapping
- 设置主机名
- 设置NIS域名
- 检查(fsck)并mount文件系统
- 打开quota
- 装载声卡模块
- 设置系统时钟
- 其它
执行完 rc.sysinit 脚本之后,系统根据运行模式选择相应的/etc/rc.d/rcx.d 。其中运行模式从/etc/inittab 文件行:
|
1
|
id:3:initdefault: |
取得。稍后会讲到每个数字代表的运行模式。
执行完 /etc/rc.d/rcn.d (这里的n 就是运行级0-6)下相应的脚本之后,就执行 /etc/rc.local 文件的内容。
再接下来等待登录,登录时会执行一系列用户环境的初始化脚本,可以参考 bashrc,bash_profile等shell配置文件详解 。
二、运行级别 runlevel 详解
1、什么是INIT
init是Linux系统操作中不可缺少的程序之一。
所谓的init进程,它是一个由内核启动的用户级进程。
内核自行启动(已经被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后,就通过启动一个用户级程序init的方式,完成引导进程。所以,init始终是第一个进程(其进程编号始终为1)。
内核会在过去曾使用过init的几个地方查找它,它的正确位置(对Linux系统来说)是/sbin/init。如果内核找不到init,它就会试着运行/bin/sh,如果运行失败,系统的启动也会失败。
2、运行级别
运行级就是操作系统当前正在运行的功能级别。这个级别从1到6 ,具有不同的功能。
不同的运行级定义如下:
- 0 – 停机(千万不能把initdefault 设置为0 )
- 1 – 单用户模式
- 2 – 多用户,没有 NFS
- 3 – 完全多用户模式,命令行文字界面
- 4 – 没有用到
- 5 – X11 多用户图形模式,采用xwindow
- 6 – 重新启动
这些级别和默认系统启动级别在/etc/inittab 文件里指定。
这个文件是init 程序寻找的主要文件,最先运行的服务是放在/etc/rc.d 目录下的文件。在大多数的Linux 发行版本中,启动脚本都是位于 /etc/rc.d/init.d中的。这些脚本被用ln 命令连接到 /etc/rc.d/rcn.d 目录。
- 查看运行级别:使用 runlevel 命令查看
|
1
2
|
[root@localhost init.d]# runlevelN 3 |
runlevel 使用的是 utmp (一般是文件 /var/run/utmp )的数据,命令输出两部分,前一位代表之前的运行级别,若没有则用 N 代替, 后一位代表示当前运行级别。
- 设置和修改运行级别的几种方式:
这些设置运行级别的方法一般不会随便使用,且都有使用惯例。
- 在启动的时候指定运行级别,在GRUB的系统选择时选择修改设置,如默认启动级别是3或5时,可以修改为 1 或其它数字代表的运行级别,1 也可以用 single 代替,这种用法一般用于系统紧急修复或特殊情况。如第二行最后添加运行级别的数字即可: (下图将进入运行级别 runlevel 1 )

- 在使用 运行级别 3 即文字界面时 要切换到 图形界面 5 ,则使用以下命令:
1
[root@localhost init.d]# startx - init 命令控制运行级别,如下:
1234
# 重启[root@localhost init.d]# init 6# 关机[root@localhost init.d]# init 0 - 修改默认运行级别,在前面已经讲过,修改 inittab 文件:
1
id:3:initdefault:
这些方法不要滥用,这些命令平常使用较少。
三 、/etc/rc.d/rcn.d 详解
我使用的系统版本是CentOS 5.8,查看 /etc/ 目录下以rc开头的文件和目录:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@localhost init.d]# ls /etc/rc.* -llrwxrwxrwx 1 root root 13 2011-04-03 /etc/rc.local -> rc.d/rc.locallrwxrwxrwx 1 root root 15 2011-04-03 /etc/rc.sysinit -> rc.d/rc.sysinit/etc/rc.d:总计 108drwxr-xr-x 2 root root 4096 07-18 15:19 init.d-rwxr-xr-x 1 root root 2255 2008-11-13 rcdrwxr-xr-x 2 root root 4096 07-18 15:19 rc0.ddrwxr-xr-x 2 root root 4096 07-18 15:19 rc1.ddrwxr-xr-x 2 root root 4096 07-19 09:20 rc2.ddrwxr-xr-x 2 root root 4096 07-19 09:20 rc3.ddrwxr-xr-x 2 root root 4096 07-19 09:20 rc4.ddrwxr-xr-x 2 root root 4096 07-19 09:20 rc5.ddrwxr-xr-x 2 root root 4096 07-18 15:19 rc6.d-rwxr-xr-x 1 root root 453 03-16 00:44 rc.local-rwxr-xr-x 1 root root 27183 2008-11-13 rc.sysinit |
可以看到 /etc/rc.sysinit 和 /etc/local 都是软链接,实际文件在 /etc/rc.d 目录下面。
其实 /etc/init.d 采用的也是类似的方式,如下所示:
|
1
2
|
[root@localhost init.d]# ls /etc/init.d -llrwxrwxrwx 1 root root 11 2011-04-03 /etc/init.d -> rc.d/init.d |
我们再来看看 /etc/rc.d/rcn.d 里面的内容,挑一个典型常用的 3 级别的目录,文件如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
[root@localhost init.d]# ls -l /etc/rc.d/rc3.d/总计 268lrwxrwxrwx 1 root root 17 2011-04-03 K01dnsmasq -> ../init.d/dnsmasqlrwxrwxrwx 1 root root 24 2011-04-03 K02avahi-dnsconfd -> ../init.d/avahi-dnsconfdlrwxrwxrwx 1 root root 24 2011-04-03 K02NetworkManager -> ../init.d/NetworkManagerlrwxrwxrwx 1 root root 16 2011-04-03 K05conman -> ../init.d/conmanlrwxrwxrwx 1 root root 19 2011-04-03 K05saslauthd -> ../init.d/saslauthdlrwxrwxrwx 1 root root 17 2011-04-03 K05wdaemon -> ../init.d/wdaemonlrwxrwxrwx 1 root root 16 2011-04-03 K10psacct -> ../init.d/psacctlrwxrwxrwx 1 root root 13 2011-04-03 K20nfs -> ../init.d/nfslrwxrwxrwx 1 root root 14 2011-04-03 K24irda -> ../init.d/irdalrwxrwxrwx 1 root root 13 2011-04-03 K35smb -> ../init.d/smblrwxrwxrwx 1 root root 19 2011-04-03 K35vncserver -> ../init.d/vncserverlrwxrwxrwx 1 root root 17 2011-04-03 K35winbind -> ../init.d/winbindlrwxrwxrwx 1 root root 20 2011-04-03 K50netconsole -> ../init.d/netconsolelrwxrwxrwx 1 root root 20 2011-04-03 K69rpcsvcgssd -> ../init.d/rpcsvcgssdlrwxrwxrwx 1 root root 16 2011-04-03 K73ypbind -> ../init.d/ypbindlrwxrwxrwx 1 root root 14 03-15 23:12 K74ipmi -> ../init.d/ipmilrwxrwxrwx 1 root root 14 2011-04-03 K74nscd -> ../init.d/nscdlrwxrwxrwx 1 root root 14 2011-04-03 K74ntpd -> ../init.d/ntpdlrwxrwxrwx 1 root root 15 2011-04-03 K80kdump -> ../init.d/kdumplrwxrwxrwx 1 root root 15 2011-04-03 K85mdmpd -> ../init.d/mdmpdlrwxrwxrwx 1 root root 20 2011-04-03 K87multipathd -> ../init.d/multipathdlrwxrwxrwx 1 root root 24 2011-04-03 K88wpa_supplicant -> ../init.d/wpa_supplicantlrwxrwxrwx 1 root root 14 2011-04-03 K89dund -> ../init.d/dundlrwxrwxrwx 1 root root 18 2011-04-03 K89netplugd -> ../init.d/netplugdlrwxrwxrwx 1 root root 14 2011-04-03 K89pand -> ../init.d/pandlrwxrwxrwx 1 root root 15 2011-04-03 K89rdisc -> ../init.d/rdisclrwxrwxrwx 1 root root 25 2011-04-03 K99readahead_later -> ../init.d/readahead_laterlrwxrwxrwx 1 root root 23 2011-04-03 S00microcode_ctl -> ../init.d/microcode_ctllrwxrwxrwx 1 root root 22 2011-04-03 S02lvm2-monitor -> ../init.d/lvm2-monitorlrwxrwxrwx 1 root root 25 07-19 09:20 S04readahead_early -> ../init.d/readahead_earlylrwxrwxrwx 1 root root 15 2011-04-03 S05kudzu -> ../init.d/kudzulrwxrwxrwx 1 root root 18 2011-04-03 S06cpuspeed -> ../init.d/cpuspeedlrwxrwxrwx 1 root root 19 2011-04-03 S08ip6tables -> ../init.d/ip6tableslrwxrwxrwx 1 root root 18 2011-04-03 S08iptables -> ../init.d/iptableslrwxrwxrwx 1 root root 18 2011-04-03 S08mcstrans -> ../init.d/mcstranslrwxrwxrwx 1 root root 17 2011-04-03 S10network -> ../init.d/networklrwxrwxrwx 1 root root 16 2011-04-03 S11auditd -> ../init.d/auditdlrwxrwxrwx 1 root root 21 2011-04-03 S12restorecond -> ../init.d/restorecondlrwxrwxrwx 1 root root 16 2011-04-03 S12syslog -> ../init.d/sysloglrwxrwxrwx 1 root root 20 2011-04-03 S13irqbalance -> ../init.d/irqbalancelrwxrwxrwx 1 root root 17 2011-04-03 S13portmap -> ../init.d/portmaplrwxrwxrwx 1 root root 24 2011-04-03 S13setroubleshoot -> ../init.d/setroubleshootlrwxrwxrwx 1 root root 17 2011-04-03 S14nfslock -> ../init.d/nfslocklrwxrwxrwx 1 root root 19 2011-04-03 S15mdmonitor -> ../init.d/mdmonitorlrwxrwxrwx 1 root root 19 2011-04-03 S18rpcidmapd -> ../init.d/rpcidmapdlrwxrwxrwx 1 root root 17 2011-04-03 S19rpcgssd -> ../init.d/rpcgssdlrwxrwxrwx 1 root root 20 2011-04-03 S22messagebus -> ../init.d/messagebuslrwxrwxrwx 1 root root 19 2011-04-03 S25bluetooth -> ../init.d/bluetoothlrwxrwxrwx 1 root root 15 2011-04-03 S25netfs -> ../init.d/netfslrwxrwxrwx 1 root root 15 2011-04-03 S25pcscd -> ../init.d/pcscdlrwxrwxrwx 1 root root 14 2011-04-03 S26hidd -> ../init.d/hiddlrwxrwxrwx 1 root root 16 2011-04-03 S28autofs -> ../init.d/autofslrwxrwxrwx 1 root root 15 2011-04-03 S44acpid -> ../init.d/acpidlrwxrwxrwx 1 root root 14 2011-04-03 S55sshd -> ../init.d/sshdlrwxrwxrwx 1 root root 14 2011-04-03 S56cups -> ../init.d/cupslrwxrwxrwx 1 root root 16 2011-04-03 S56xinetd -> ../init.d/xinetdlrwxrwxrwx 1 root root 18 2011-04-03 S80sendmail -> ../init.d/sendmaillrwxrwxrwx 1 root root 13 2011-04-03 S85gpm -> ../init.d/gpmlrwxrwxrwx 1 root root 15 2011-04-03 S90crond -> ../init.d/crondlrwxrwxrwx 1 root root 13 2011-04-03 S90xfs -> ../init.d/xfslrwxrwxrwx 1 root root 17 2011-04-03 S95anacron -> ../init.d/anacronlrwxrwxrwx 1 root root 13 2011-04-03 S95atd -> ../init.d/atdlrwxrwxrwx 1 root root 15 2011-04-03 S97rhnsd -> ../init.d/rhnsdlrwxrwxrwx 1 root root 22 2011-04-03 S97yum-updatesd -> ../init.d/yum-updatesdlrwxrwxrwx 1 root root 22 2011-04-03 S98avahi-daemon -> ../init.d/avahi-daemonlrwxrwxrwx 1 root root 19 2011-04-03 S98haldaemon -> ../init.d/haldaemonlrwxrwxrwx 1 root root 19 2011-04-03 S99firstboot -> ../init.d/firstbootlrwxrwxrwx 1 root root 11 2011-04-03 S99local -> ../rc.locallrwxrwxrwx 1 root root 16 2011-04-03 S99smartd -> ../init.d/smartd |
可以看到 这里面的文件都是以K或S开头,然后接一个数字,再接上一个服务名称,而且都是/etc/rc.d/init.d 目录下的文件的软链接。S表示启动,K表示杀死,数字表示启动顺序,数字来自于 /etc/rc.d/init.d 脚本内部,表示顺序,从小到大依次执行。
四、init.d 脚本详解
我们选一个 /etc/init.d/ 目录下比较简单的脚本来分析,如readahead_early ,这个脚本的内容如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#!/bin/sh## readahead: Prereads programs required for startup into memory## chkconfig: 2345 4 99# description: This service causes the programs used during startup \# to be loaded into memory before they are needed,\# thus improving startup performance### Sanity checks.[ -x /usr/sbin/readahead ] || exit 0# Check for > 384 MBfree -m | gawk '/Mem:/ {exit ($2 >= 384)?0:1}' || exit 0# Source function library.. /etc/rc.d/init.d/functionsstart() { echo -n $"Starting background readahead: " FILES=$( ls /etc/readahead.d/*.early ) /usr/sbin/readahead $FILES >/dev/null & echo_success echo}stop() { /bin/true}# See how we were called.case "$1" in start) start ;; stop) stop ;; status) ;; restart) stop start ;; condrestart) ;; reload) ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}" ;;esacexit $RETVAL |
注意以下几个地方:
- 在注释中包括
1
# chkconfig: 2345 4 99这一行是必需的,其中 2345 表示运行级别,如果这个服务默认在任何级别都不启动,则使用 – 代替。4 表示启动顺序,99 表示杀死顺序。
- 注释中的描述必须包含,多行描述在行尾使用 \ ,如下:
123
# description: This service causes the programs used during startup \# to be loaded into memory before they are needed,\# thus improving startup performance - . /etc/rc.d/init.d/functions 行,这是包含 /etc/rc.d/init.d/functions 这个文件,这个文件提供了一些函数供使用,如echo_success(), echo_failure()
- 脚本一般包含 start(), stop(), restart(), status() 这几个标准函数
- 脚本使用一个 case 选择操作符来进行相应的操作
- $RETVAL 表示返回值
在上面的 readahead_early 可能无法体现 $RETVAL 的作用,在下面这个yum-updatesd 这个服务中可以清楚看到:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#!/bin/bash## yum Update notification daemon## Author: Jeremy Katz <katzj@redhat.com>## chkconfig: 345 97 03## description: This is a daemon which periodically checks for updates \# and can send notifications via mail, dbus or syslog.# processname: yum-updatesd# config: /etc/yum/yum-updatesd.conf# pidfile: /var/run/yum-updatesd.pid#### BEGIN INIT INFO# Provides: yum-updatesd# Required-Start: $syslog $local_fs messagebus# Required-Stop: $syslog $local_fs messagebus# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Short-Description: Update notification daemon# Description: Daemon which notifies about available updates via mail, dbus or# syslog. Can also be configured to automatically apply updates.### END INIT INFO# source function library. /etc/rc.d/init.d/functionsRETVAL=0start() { echo -n $"Starting yum-updatesd: " daemon +19 'yum-updatesd &' RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/yum-updatesd}stop() { echo -n $"Stopping yum-updatesd: " killproc yum-updatesd echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/yum-updatesd}restart() { stop start}case "$1" in start) start ;; stop) stop ;; restart|force-reload|reload) restart ;; condrestart|try-restart) [ -f /var/lock/subsys/yum-updatesd ] && restart ;; status) status yum-updatesd RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}" exit 1esacexit $RETVAL |
至此,你应该对 rc.d rc.local rc.sysinit 这些脚本有了全盘的了解与认识了。

浙公网安备 33010602011771号