用法:

chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接-

  使用语法:

  chkconfig[--add][--del][--list][系统服务]或chkconfig[--level<等级代号>][系统服务][on/off/reset]

  chkconfig在没有参数运行时,显示用法。如果加上服务名,那么就检查这个服务是否在当前运行级启动。如果是,返回true,否则返回false。如果在服务名后面指定了on,off或者reset,那么chkconfi会改变指定服务的启动信息。on和off分别指服务被启动和停止,reset指重置服务的启动信息,无论有问题的初始化脚本指定了什么。on和off开关,系统默认只对运行级3,4,5有效,但是reset可以对所有运行级有效。

  参数用法:

  --add增加所指定的系统服务,让chkconfig指令得以管理它,并同时在系统启动的叙述文件内增加相关数据。

  --del删除所指定的系统服务,不再由chkconfig指令管理,并同时在系统启动的叙述文件内删除相关数据。

  --level<等级代号>指定读系统服务要在哪一个执行等级中开启或关毕。

  等级0表示:表示关机

  等级1表示:单用户模式

  等级2表示:无网络连接的多用户命令行模式

  等级3表示:有网络连接的多用户命令行模式

  等级4表示:不可用

  等级5表示:带图形界面的多用户模式

  等级6表示:重新启动

  需要说明的是,level选项可以指定要查看的运行级而不一定是当前运行级。对于每个运行级,只能有一个启动脚本或者停止脚本。当切换运行级时,init不会重新启动已经启动的服务,也不会再次去停止已经停止的服务。

  chkconfig--list[name]:显示所有运行级系统服务的运行状态信息(on或off)。如果指定了name,那么只显示指定的服务在不同运行级的状态。

  chkconfig--addname:增加一项新的服务。chkconfig确保每个运行级有一项启动(S)或者杀死(K)入口。如有缺少,则会从缺省的init脚本自动建立。

  chkconfig--delname:删除服务,并把相关符号连接从/etc/rc[0-6].d删除。

  chkconfig[--levellevels]name:设置某一服务在指定的运行级是被启动,停止还是重置。

  运行级文件:

  每个被chkconfig管理的服务需要在对应的init.d下的脚本加上两行或者更多行的注释。第一行告诉chkconfig缺省启动的运行级以及启动和停止的优先级。如果某服务缺省不在任何运行级启动,那么使用-代替运行级。第二行对服务进行描述,可以用\跨行注释。

  例如,random.init包含三行:

  #chkconfig:23452080

  #description:Savesandrestoressystementropypoolfor\

  #higherqualityrandomnumbergeneration.

  使用范例:

  chkconfig--list#列出所有的系统服务

  chkconfig--addhttpd#增加httpd服务

  chkconfig--delhttpd#删除httpd服务

  chkconfig--levelhttpd2345on#设置httpd在运行级别为2、3、4、5的情况下都是on(开启)的状态

  chkconfig--list#列出系统所有的服务启动情况

  chkconfig--listmysqld#列出mysqld服务设置情况

  chkconfig--level35mysqldon#设定mysqld在等级3和5为开机运行服务,--level35表示操作只在等级3和5执行,on表示启动,off表示关闭

  chkconfigmysqldon#设定mysqld在各等级为on,“各等级”包括2、3、4、5等级

  如何增加一个服务:

  1.服务脚本必须存放在/etc/ini.d/目录下;

  2.chkconfig--addservicename

  在chkconfig工具服务列表中增加此服务,此时服务会被在/etc/rc.d/rcN.d中赋予K/S入口了;

  3.chkconfig--level35mysqldon

  修改服务的默认启动等级。

应用实例
1. chkconfig脚本格式:
#!/bin/sh
#chkconfig 2345 55 45
#上面为固定格式:2345 表示运行级别,55表示开机执行顺序,45为关机顺序
#description:this is just a demo of chkconfig script
case “$1” in
start)
<start-script>
;;
Stop)
<stop-script>
;;
Status)
Echo <the information you want to display>
;;
*)
Echo “the usage of the script”
Case
2. 然后将脚本保存,并赋予执行权限,再复制到/etc/init.d目录
#chmod a+x <myscript>
#copy <myscript> /etc/init.d
3. 使用chkconfig命令添加成服务
#chkconfig --add <myscript>
#chkconfig --level 35 <myscript > on
#chkconfig --list <myscript>
4. 然后就可以通过service命令管理了
#service <myscript> start | stop | status
5. 下面是我写的一个实例脚本,大家可以参考一些格式:
#!/bin/sh
#chkconfig: 2345 99 99
#description:the script to set the network at run level 2345
IN=eth0
OUT=eth1
HOST_NAME=cluster1.yang.com
INIP=192.168.10.10
OUTIP=192.168.136.10
MASK=255.255.255.0
IP=/sbin/ip
IFC=/sbin/ifconfig
ROUTE=/sbin/route
#flush the address
case "$1" in
start)
#echo "flush the address..."
#$IP addr flush dev eth0
#$IP addr flush dev eth1
echo "set the address..."
$IFC $IN $INIP netmask $MASK up
$IFC $OUT $OUTIP netmask $MASK up
echo "set the hostname..."
hostname $HOST_NAME
echo "set the default gateway..."
$IP route flush all
$ROUTE add default gw 192.168.136.2
echo "finshed!!!"
;;
stop)
echo "flush the network setting..."
$IP addr flush dev eth0
$IP addr flush dev eth1
echo "flush finshed!!!"
;;
status)
echo "hostname is $HOST_NAME"
$IFC eth0
$IFC eth1
;;
*)
echo "requires start,stop or status"
;;
esac
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------

在Linux中chkconfighttpd任务添加,Apache服务器的最新稳定发布版本是httpd-2.2..0,官方下载地址是:http://httpd.apache.org/download.cgi。我们通过下面的步骤来快速的搭建一个web服务器。

1、下载源码文件httpd-2.2.0.tar.gz 到linux服务器的某个目录。
2、解压文件 # tar zxvf httpd-2.2.0.tar.gz .
3、配置 # ./configure –refix=/usr/local/apache //指定安装目录,以后要删除安装就只需删除这个目录。
4、编译和安装。 # make ; make install .
5、编写启动脚本,把它放到目录 /etc/rc.d/init.d/里,这里取名为httpd,其内容如下:

#!/bin/bash  

#description:http server  

#chkconfig: 235 98 98  

case "$1" in  

start)  

echo "Starting Apache daemon..."  

/usr/local/apache2/bin/apachectl -k start  

;;  

stop)  

echo "Stopping Apache daemon..."  

/usr/local/apache2/bin/apachectl -k stop  

;;  

restart)  

echo "Restarting Apache daemon..."  

/usr/local/apache2/bin/apachectl -k restart  

;;  

status)  

statusproc /usr/local/apache2/bin/httpd  

;;  

*)  

echo "Usage: $0 {start|stop|restart|status}"  

exit 1  

;;  

Esac  

注意:#description:http server 这一行必须加上,否则在执行命令

# chkconfig –add httpd

时会出现“service apache does not support chkconfig”的错误报告。

#chkconfig: 2345 98 98 表示在执行命令

# chkconfig –add httpd 时会在目录 /etc/rc2.d/ 、/etc/rc3.d/ /etc/rc5.d 分别生成文件 S98httpd和 K98httpd。这个数字可以是别的。

6、执行命令 # chkconfig –add httpd ,进入目录/etc/rc3.d/检查是否生成文件 S98httpd及K98httpd.
7、启动服务 # service httpd start .