[Linux运维] 基本服务rc
2015-09-04 12:21
603 查看
[Linux运维] 基本服务rc
rc = 'run command',执行命令,通常linux启动的时候,/etc/rc是linux启动的主脚本,而.bashrc是当Linux的bash shell启动后所运行的脚本。今天我们来看一看/etc/rc下面是什么东西,也就是linux启动的时候都干了什么。1. redhat 启动过程
加载BIOS读取MBR
Boot loader
加载内核
执行init程序,
/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的脚本
执行login程序,进入登陆状态
2. 运行等级
linux有7个运行等级,具体如下: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.
(1) /etc/inittab
系统初始化运行等级的时候,就是读的这个文件$ 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:,这个就是上面说到的确定运行等级,就是看这个这里。
(2)看当前的runlevel
$ /sbin/runlevel N 3
runlevel - output previous and current runlevel
(3) 切换runlevel
$ telinit N
telinit - change system runlevel
3. rcN.d/ 介绍
init 程序会根据runlevel去执行 相应rcN.d(0 <= N <= 6) 目录下面的内容。一般runlevel 3是最常用的。$ 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下面的。
简单看一下里面的内容
$ 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的时候运行的命令,数字表示优先级,数字小的先执行
4. /etc/rc.local
Redhat中的运行模式2、3、5都把/etc/rc.d/rc.local做为初始化脚本中的最后一个,所以用户可以自己在这个文件中添加一些需要在其他初始化工作之后,登录之前执行的命令。比如开机自启动的东西,当然这里,最好使用nohup &,防止卡主。
5. 设置服务的开机自启动 -- chkconfig
(1)chkconfig
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:设置某一服务在指定的运行级是被启动,停止还是重置。
(2) 怎样使用chkconfig 添加服务
条件是:a) 服务脚本必须存放在/etc/ini.d/目录下;
b) 服务脚本里面要有这两行
# chkconfig: 2345 20 80
# description: XXXXX
上面的第一行里面,2345是运行等级,20是启动优先级,80是停止优先级,这是chkconfig添加或者修改服务的时候需要的。
(3) demo
$ 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的软链。
修改hello的启动等级
$ 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消失了。
6. 参考:
(1)http://comptechdoc.org/os/linux/startupman/linux_surcsysinit.html(2)http://www.xshell.net/linux/inittab_rc.html
相关文章推荐
- 在Ubuntu(Linux)上获取root权限
- arm平台移植mplayer
- Linux系统管理-(8)-RPM程序包管理器
- CentOS 7 挂载本地光盘作为镜像源
- 【Linux】【C/C++】多进程协同词频统计
- Basic Linux Commands :date, clock, hwclock, cal, ls, pwd, whereis, which, who, w, whoami
- centos6安装bt工具transmission
- centos6安装bt工具transmission
- Linux内核中fasync机制对RCU锁的使用
- 【Linux】【C/C++】十叉非完全树的构造和重构
- 如何成为一个Linux内核开发者
- Linux 下以其他用户运行程序
- 10分钟掌握Linux vi编辑器常见命令的使用,最简单的vi编辑器教程
- linux screen 命令详解
- 20150825-Linux用户管理和权限管理
- linux输入输出重定向命令
- Linux下的Nutch 1.4 安装配置
- linux vi编辑器操作手册
- linux运维实战练习-2015年8月30日课程作业
- linux下gcc的安装