[svc]centos6使用chkconfig治理服务和其原理

centos6开机启动级别

$ cat /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启动过程

chkconfig默认管理345级别开机启动

参考


chkconfig --list        #列出所有的系统服务
chkconfig --add httpd        #增加httpd服务
chkconfig --del httpd        #删除httpd服务
chkconfig --level httpd 2345 on        #设置httpd在运行级别为2、3、4、5的情况下都是on(开启)的状态
chkconfig --list        #列出系统所有的服务启动情况
chkconfig --list mysqld        #列出mysqld服务设置情况
chkconfig --level 35 mysqld on        #设定mysqld在等级3和5为开机运行服务,--level 3

chkconfig的原理

如何将自己的服务,托管给chkconfig管理?

1,脚本放在/etc/init.d下面,可以执行(/etc/init.d/sshd),
希望被chkconfig管理,需要添加到chkconfig --add sshd

2.chkconfig --level 3 sshd on,本质上市
rm -f /etc/rc.d/rc3.d/K25sshd
ln -s /etc/init.d/sshd /etc/rc.d/rc3.d/S55sshd

3.chkconfig --level 3 sshd off
rm -f /etc/rc.d/rc3.d/S55sshd
ln -s /etc/init.d/sshd /etc/rc.d/rc3.d/K25sshd

4. /etc/init.d/sshd里面设置了对应的级别启动顺序
# chkconfig: 2345 55 25
# description: SSH is a protocol...

5.开发自己的程序,让chkconfig管理
$ cat /etc/init.d/maotai
#!/bin/bash

# chkconfig: 2345 56 26
# description: maotai service test...

case "$1" in
    start)
        action "maotai is started" /bin/true
    ;;
    *)
        echo "$0 USAGE{start|stop|restart}"

chkconfig --add/del maotai

小结:

  • 1.把重要命令加到/etc/rc.local也能达到让启动命令开机时候启动目的.
  • 2.添加一个服务,比如3级别的nfs自启动,chkconfig相当于在/etc/init.d/rc3.d目录里做个软链接:S60nfs
  • 3.软链接的首字母为"K",表示关机时候会会关闭,为"S"表示开机时候会启动,数字60为开机服务启动的顺序,这个数字可以通过chkconfig控制的启动脚本的开头有明确的声明.
  • 4.添加,删除系统自启动服务的本质,实际上是在/etc/init.d/rc3.d穿件启动与删除脚本的软链接.

生产例子: 精简linux开机自启动项

- 需要启动项有:
rsyslog
sshd
network
crond
sysstat


- sysstat软件包集成的主要工具为:
	iostat:提供cpu使用率及硬盘吞吐效率的数据
	mpstat:工具提供与单个或多个处理器相关的数据
	sar   :工具负责收集,报告,病存储系统活跃的信息.
思路:
1,关掉所有,启动想启动的
2,启动想启动的,关闭其他的
## awk过滤行,之后拼凑字符串
[root@n1 ~]# cat svc.txt |awk '{print "chkconfig",$1,"on"}'
chkconfig crond on
chkconfig dbus on
chkconfig docker on
chkconfig getty@tty1 on
chkconfig httpd on
chkconfig kmod-static-nodes on
chkconfig lvm2-lvmetad on
chkconfig lvm2-pvscan@8:2 on
chkconfig network on
chkconfig ntpdate on
chkconfig polkit on
chkconfig rhel-dmesg on
chkconfig rhel-import-state on
chkconfig rhel-readonly on
chkconfig rsyslog on
chkconfig sshd on

- 关掉所有,启动想启动的
[root@n1 ~]# cat svc.txt|grep 3:on|awk '{print "chkconfig",$1,"off"}'|bash #命令交给bash执行
[root@n1 ~]# cat svc.txt|grep -E "rsyslog|sshd|network|crond|sysstat" |awk '{print "chkconfig",$1,"on"}'
chkconfig crond on
chkconfig network on
chkconfig rsyslog on
chkconfig sshd on

- 启动想启动的,关闭其他的
[root@n1 ~]# cat svc.txt|grep -E "rsyslog|sshd|network|crond|sysstat" |awk '{print "chkconfig",$1,"on"}'
[root@n1 ~]# cat svc.txt|grep -vE "rsyslog|sshd|network|crond|sysstat" |awk '{print "chkconfig",$1,"off"}'
chkconfig dbus off
chkconfig docker off
chkconfig getty@tty1 off
chkconfig httpd off
chkconfig kmod-static-nodes off
chkconfig lvm2-lvmetad off
chkconfig lvm2-pvscan@8:2 off
chkconfig ntpdate off
chkconfig polkit off
chkconfig rhel-dmesg off
chkconfig rhel-import-state off
chkconfig rhel-readonly off
posted @ 2018-03-04 09:48  _毛台  阅读(355)  评论(0编辑  收藏  举报