手机版

7个Linux chkconfig命令实例

时间:2020-02-19 来源:互联网 编辑:宝哥软件园 浏览:

Chkconfig命令用来设置,查看或更改配置开机自动启动的服务。下面根据七个实用的实例来说明Chkconfig命令的使用方法。

1、用Shell脚本检测服务系统启动项状态

当你只用服务名执行chkconfig命令时,如果该服务已经配置到系统启动项即返回真。下面的代码段是用来检查一个服务是否已经配置开机启动。

# vi check.sh
chkconfig network && echo "Network service is configured"
chkconfig junk && echo "Junk service is configured"
 
# ./check.sh
Network service is configured

你也可以指定检查该服务是否配置到指定的运行级。

# vi check1.sh
chkconfig network --level 3 && echo "Network service is configured for level 3"
chkconfig network --level 1 && echo "Network service is configured for level 1"
 
# ./check1.sh
Network service is configured for level 3

2、查看当前服务系统启动项的状态

–list选项用来显示当前所有服务的系统启动项状态。

# chkconfig --list
abrtd   0:off   1:off   2:off   3:on    4:off   5:on    6:off
acpid   0:off   1:off   2:off   3:off   4:off   5:off   6:off
atd     0:off   1:off   2:off   3:on    4:on    5:on    6:off
...

可以使用grep过滤显示指定条件的服务。
下面的命令表示只显示运行级为3的服务。

chkconfig --list | grep 3:on

下面的命令表示只显示network服务的启动项状态。

chkconfig --list | grep network

3、添加一个新服务到启动项

使用–add选项来添加一个指定服务到系统启动服务列表。
下面的例子说明如何添加一个新服务(如iptables)到需要开机启动的服务列表。“chkconfig –add”命令也会自动地开启运行级2,3,4和5,如下:

# chkconfig --list | grep iptables
# chkconfig --add iptables
# chkconfig --list | grep iptables
iptables       0:off   1:off   2:on    3:on    4:on    5:on    6:off

4、从系统启动项列表删除一个服务

下面的例子表明ip6tables已经配置到启动项。

# chkconfig --list | grep ip6tables
ip6tables       0:off   1:off   2:off   3:on   4:off   5:off   6:off

使用–del选项从启动列表删除它。

# chkconfig --del ip6tables
# chkconfig --list | grep ip6tables

5、为服务开启或关闭选定的运行级

有时候你可能不想从启动列表中删除整个服务,然而你可能仅仅想关闭指定的运行级。
下面的例子是为nfserver服务关闭运行级5.

# chkconfig --level 5 nfsserver off

你也可以同时关闭多个运行级,下面是关闭3和5运行级。

# chkconfig --level 35 nfsserver off

6、rc.d子目录下的脚本文件

无论你什么时候使用chkconfig命令添加或删除一个服务,它都会在/etc/rc.d子目录执行某些动作。

当chkconfig –add命令被执行,它会在对应的rc目录创建一个符合链接文件来启动和停止服务。 当chkconfig –del命令被执行,它会在对应的rc目录删除相应的符号链接。

下面的例子表明xinetd服务已经开启运行级3和5,所以xinetd会有两个文件在rc3.d目录和两个文件在rc5.d目录。以K开头的文件关机的时候用(K表示kill)。以S开头的文件开机的时候用(S表示start)。

# chkconfig --list | grep xinetd
xinetd                    0:off  1:off  2:off  3:on   4:off  5:on   6:off
xinetd based services:
 
# cd /etc/rc.d/rc3.d
# ls | grep xinetd
K08xinetd
S14xinetd
 
# cd /etc/rc.d/rc5.d
 
# ls | grep xinetd
K08xinetd
S14xinetd

7、添加操作使rcx.d目录的变化

当你通过chkconfig命令添加一个新服务,默认的运行级会自动地为该服务开启,并且会在对应的rcx目录创建文件。
例如,如果nfsserver服务没有在启动项列表中,那nfsserver服务就没有文件在/etc/rc.d/rc*.d目录下。

# chkconfig  --list | grep nfsserver
nfsserver                 0:off  1:off  2:off  3:off  4:off  5:off  6:off
# ls /etc/rc.d/rc3.d | grep nfsserver
# ls /etc/rc.d/rc5.d | grep nfsserver

当你添加nfsserver服务之后,你将会在这些目录下看到符号链接。

# chkconfig --add nfsserver
nfsserver                 0:off  1:off  2:off  3:on   4:off  5:on   6:off
 
# cd  /etc/rc.d/rc3.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver
 
# cd /etc/rc.d/rc5.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver

当你使用–del或–level选项来关闭服务,在rcx.d目录下对应的符号链接文件将会被删除。

# chkconfig --level 5 nfsserver off
# ls /etc/rc.d/rc5.d  | grep nfsserver

版权声明:7个Linux chkconfig命令实例是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。

相关文章推荐