chkconfig - updates and queries runlevel information for system services
首先我们得知道Linux的7个运行级别,查看/etc/inittab文件,就可以看到对应的描述
# 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)
(关于Linux的运行级别,在某一次面试的过程中还出过丑,当时面试官问我级别6是做什么用的,我当时就傻蒙了,平时见到的也就是0,1,3,5,重启都是用reboot的,所以基础不扎实很可怕的)
熟悉了7个运行级别,那么对于rc.0~rc.6这7个目录就能有直观的认识了,没错,这7个目录就对应7个运行级别下的启动或者关闭系统时需要start或者stop的服务脚本,如果仔细看,会发现这7个目录下的脚本其实都是/etc/init.d/或者/etc/rc.d/init.d下面脚本的软链接(这两个目录也是链接关系)。有两个文件/etc/rc.d/rc.local和/etc/rc.d/rc.sysinit需要单独对待,它们分别是用户定定义启动内容和系统自己初始化用的脚本,其中/etc/rc.d/rc.sysinit可以在/etc/inittab中找到踪迹
在对应级别的启动目录中,分别有S和K开头的启动脚本,对应在开机时此级别下应该启动或者在关机时应该结束的服务。每个文件名中都有一个数字表示运行的优先级,数字越小则优先级越高,例如S25netfs的运行级别要比S95atd高,也就是说netfs要优先于atd启动。还有一个比较有意思的地方就是rc目录下的S或者K脚本,都是软连接到/etc/rc.d/init.d下面对应的脚本的,所以我理解是不是系统在执行rc[0-6]下面的脚本时,只是通过S或者K传递start或者stop参数给/etc/rc.d/init.d下面的真实脚本。
经过上面的一番认识,我们就很清楚怎么配置service的开机运行了,没错,就是在对应级别的启动目录下放置自己的服务启动脚本,如果是系统自带的服务,就可以利用chkconfig自动完成软链接的建立过程。当然了chkconfig还可以将自己的服务添加到系统服务列表中以便管理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# chkconfig chkconfig version 1.3.30.2 - Copyright (C) 1997-2000 Red Hat, Inc. This may be freely redistributed under the terms of the GNU Public License. usage: chkconfig --list [name] chkconfig --add <name> chkconfig --del <name> chkconfig [--level <levels>] <name> <on|off|reset|resetpriorities> |
可见chkconfig的选项还是比较少的,最常用的应该还是 --level设置服务启动与否,下面按照鸟哥的介绍,逐个的演练一下。
"- -list",例举当前各项服务的状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# chkconfig --list NetworkManager 0:off 1:off 2:off 3:off 4:off 5:off 6:off acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off aksusbd 0:off 1:off 2:on 3:on 4:on 5:on 6:off anacron 0:off 1:off 2:on 3:on 4:on 5:on 6:off apmd 0:off 1:off 2:on 3:on 4:on 5:on 6:off atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off autofs 0:off 1:off 2:off 3:on 4:on 5:on 6:off avahi-daemon 0:off 1:off 2:off 3:on 4:on 5:on 6:off |
"- -level",设定某个服务在该level下启动或者关闭
1 2 3 4 5 6 7 |
# chkconfig --level 35 atd off # ls -al | grep S95 lrwxrwxrwx 1 root root 17 Jun 11 2012 S95anacron -> ../init.d/anacron |
可以看到在rc3.d下面已经没有了S95atd了,再次开启atd
1 2 3 4 5 6 7 8 9 |
# chkconfig --level 35 atd on # ls -al | grep S95 lrwxrwxrwx 1 root root 17 Jun 11 2012 S95anacron -> ../init.d/anacron lrwxrwxrwx 1 root root 13 Mar 29 22:21 S95atd -> ../init.d/atd |
S95atd又回来了
"- -add和- -del",添加或者删除服务
首先在/etc/rc.d/init.d下面建立一个自己的服务脚本,取名myservice,输入如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/bin/sh #chkconfig的语法为 # chkconfig:[runlevel] [start number] [stop number] # chkconfig: 35 80 70 # description: this is service for chkconfig testing echo "My Service" |
然后添加服务
1 2 3 4 5 6 7 |
# chkconfig --add myservice # chkconfig --list | grep myservice myservice 0:off 1:off 2:off 3:on 4:off 5:on 6:off |
这里level3和level5默认是开启的,搜索一下rc.d下面所有的myservic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# find /etc/rc.d/ -type l | grep "myservice"|sort /etc/rc.d/rc0.d/K70myservice /etc/rc.d/rc1.d/K70myservice /etc/rc.d/rc2.d/K70myservice /etc/rc.d/rc3.d/S80myservice /etc/rc.d/rc4.d/K70myservice /etc/rc.d/rc5.d/S80myservice /etc/rc.d/rc6.d/K70myservice |
接下来删除myservice
1 2 3 4 5 6 7 8 9 |
#chkconfig --del myservice # rm /etc/init.d/myservice rm: remove regular file `/etc/init.d/myservice'? y # |
整个chkconfig配置基本上就是上面的这些内容,测试都是在CentOS5.6 x86版本上进行的,对于不同的发行版本,或许rc.d和init.d目录结构有些差别,但是明白了运行level和rc目录之间的关系,在修改启动项时就会清楚明了了。
(在编辑的时候可以显示两个"-",为什么发布后就变为"-"了呢,难道是一个bug,无奈只好在中间补个空格)