rc = 'run command',执行命令,一般linux启动的时候,/etc/rc是linux启动的主脚本,而.bashrc是当Linux的bash shell启动后所运行的脚本。今天咱们来看一看/etc/rc下面是什么东西,也就是linux启动的时候都干了什么。php
/sbin/init
,pid为1的进程/etc/inittab
/etc/rc.d/rc.sysinit
,这是init进程执行的第一个脚本,内容不少,能够参考 参考1的内容。/etc/modules.conf
文件或/etc/modules.d
目录下的文件来装载内核模块/etc/rc0.d/
-- /etc/rc6.d/
/etc/rc.d/rc.local
的脚本linux有7个运行等级,具体以下:html
0 Halt: Shuts down the system.
1 Single-user mode: Mode for administrative tasks.
2 Multiuser mode: Does not configure network interfaces and does not export networks services.
3 Multiuser mode with networking: Starts the system normally.
4 Not used/user-definable For special purposes.
5 Start the system normally with appropriate display manager (with GUI) Same as runlevel 3 + display manager.
6 Reboot Reboots the system.node
系统初始化运行等级的时候,就是读的这个文件linux
$ cat /etc/inittab # inittab is only used by upstart for the default runlevel. # # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # System initialization is started by /etc/init/rcS.conf # # Individual runlevels are started by /etc/init/rc.conf # # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf # # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, # with configuration in /etc/sysconfig/init. # # For information on how to write upstart event handlers, or how # upstart works, see init(5), init(8), and initctl(8). # # Default runlevel. The runlevels used are: # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) # 3 - Full multiuser mode # 4 - unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) # id:3:initdefault:
这个文件里面也有相应的运行等级的介绍,咱们主要关注 id:3:initdefault:
,这个就是上面说到的肯定运行等级,就是看这个这里。ios
$ /sbin/runlevel N 3
runlevel - output previous and current runlevelshell
$ telinit N
telinit - change system runlevelbash
init 程序会根据runlevel去执行 相应rcN.d(0 <= N <= 6) 目录下面的内容。通常runlevel 3是最经常使用的。app
$ ls -l rc*.d lrwxrwxrwx 1 root root 10 Nov 3 2014 rc0.d -> rc.d/rc0.d lrwxrwxrwx 1 root root 10 Nov 3 2014 rc1.d -> rc.d/rc1.d lrwxrwxrwx 1 root root 10 Nov 3 2014 rc2.d -> rc.d/rc2.d lrwxrwxrwx 1 root root 10 Nov 3 2014 rc3.d -> rc.d/rc3.d lrwxrwxrwx 1 root root 10 Nov 3 2014 rc4.d -> rc.d/rc4.d lrwxrwxrwx 1 root root 10 Nov 3 2014 rc5.d -> rc.d/rc5.d lrwxrwxrwx 1 root root 10 Nov 3 2014 rc6.d -> rc.d/rc6.d
咱们看到/etc/rcN.d 都是软链到/etc/rc.d/rcN.d下面的。运维
简单看一下里面的内容ssh
$ ls /etc/rc.d/rc0.d/ K01gshelld K04salt-minion K15collectd K16php-fpm K50netconsole K73winbind K75netfs K87rpcbind K89rdisc K99salt-syndic K01nagios K05salt-master K15htcacheclean K25sshd K50vsftpd K74lm_sensors K75ntpdate K88auditd K90network K99sysstat K01node_service K10saslauthd K15httpd K30nrpe K60crond K74nscd K75udev-post K88rsyslog K92ip6tables S00killall K01tbscand K10tornado K15svnserve K30postfix K70yum-updateonboot K74ntpd K87restorecond K89portreserve K92iptables S01halt
其中 S表示start的时候运行的命令,K表示shutdown的时候运行的命令,数字表示优先级,数字小的先执行
Redhat中的运行模式二、三、5都把/etc/rc.d/rc.local作为初始化脚本中的最后一个,因此用户能够本身在这个文件中添加一些须要在其余初始化工做以后,登陆以前执行的命令。好比开机自启动的东西,固然这里,最好使用nohup &
,防止卡主。
chkconfig命令主要用来更新(启动或中止)和查询系统服务的运行级信息。谨记chkconfig不是当即自动禁止或激活一个服务,它只是简单的改变了符号链接。
chkconfig --list [name]:显示全部运行级系统服务的运行状态信息(on或off)。若是指定了name,那么只显示指定的服务在不一样运行级的状态。
chkconfig --add name:增长一项新的服务。chkconfig确保每一个运行级有一项启动(S)或者杀死(K)入口。若有缺乏,则会从缺省的init脚本自动创建。
chkconfig --del name:删除服务,并把相关符号链接从/etc/rc[0-6].d删除。
chkconfig [--level levels] name:设置某一服务在指定的运行级是被启动,中止仍是重置。
条件是:
a) 服务脚本必须存放在/etc/ini.d/目录下;
b) 服务脚本里面要有这两行
# chkconfig: 2345 20 80
# description: XXXXX
上面的第一行里面,2345是运行等级,20是启动优先级,80是中止优先级,这是chkconfig添加或者修改服务的时候须要的。
$ cat /etc/init.d/hello #! /bin/bash # chkconfig: 2345 20 80 # description: Just test for chkconfig echo "hello world"
$ chkconfig --add hello $ chkconfig --list | grep hello hello 0:off 1:off 2:on 3:on 4:on 5:on 6:off
咱们如今看一下rc.d目录下面的状况:
$ find rc.d/ -name '*hello' | xargs ls -lh -rwxr-xr-x 1 root root 97 Sep 3 23:55 rc.d/init.d/hello lrwxrwxrwx 1 root root 15 Sep 3 23:55 rc.d/rc0.d/K80hello -> ../init.d/hello lrwxrwxrwx 1 root root 15 Sep 3 23:55 rc.d/rc1.d/K80hello -> ../init.d/hello lrwxrwxrwx 1 root root 15 Sep 3 23:55 rc.d/rc2.d/S20hello -> ../init.d/hello lrwxrwxrwx 1 root root 15 Sep 3 23:55 rc.d/rc3.d/S20hello -> ../init.d/hello lrwxrwxrwx 1 root root 15 Sep 3 23:55 rc.d/rc4.d/S20hello -> ../init.d/hello lrwxrwxrwx 1 root root 15 Sep 3 23:55 rc.d/rc5.d/S20hello -> ../init.d/hello lrwxrwxrwx 1 root root 15 Sep 3 23:55 rc.d/rc6.d/K80hello -> ../init.d/hello
咱们看到如今在rc2-5.d/的目录下面多一个S20hello的软链,其余等级下面有K80hello的软链。
$ chkconfig --level 23 hello off $ chkconfig --list | grep hello hello 0:off 1:off 2:off 3:off 4:on 5:on 6:off # find /etc/rc.d/ -name '*hello' | xargs ls -lh -rwxr-xr-x 1 root root 97 Sep 3 23:55 /etc/rc.d/init.d/hello lrwxrwxrwx 1 root root 15 Sep 3 23:55 /etc/rc.d/rc0.d/K80hello -> ../init.d/hello lrwxrwxrwx 1 root root 15 Sep 3 23:55 /etc/rc.d/rc1.d/K80hello -> ../init.d/hello lrwxrwxrwx 1 root root 15 Sep 4 12:12 /etc/rc.d/rc2.d/K80hello -> ../init.d/hello lrwxrwxrwx 1 root root 15 Sep 4 12:12 /etc/rc.d/rc3.d/K80hello -> ../init.d/hello lrwxrwxrwx 1 root root 15 Sep 3 23:55 /etc/rc.d/rc4.d/S20hello -> ../init.d/hello lrwxrwxrwx 1 root root 15 Sep 3 23:55 /etc/rc.d/rc5.d/S20hello -> ../init.d/hello lrwxrwxrwx 1 root root 15 Sep 3 23:55 /etc/rc.d/rc6.d/K80hello -> ../init.d/hello
咱们看到等级2,3变成了K80hello,而原先的S20hello消失了。
(1)http://comptechdoc.org/os/linux/startupman/linux_surcsysinit.html
(2)http://www.xshell.net/linux/inittab_rc.html