Posted on 2009-04-28 10:30 
希 阅读(1221) 
评论(1)  编辑  收藏  
			
			
		 
		在linux中,使用chkconfig命令,加入系统服务,实现软件的自动启动。
1. 自启动脚本模板
*****************下面的是脚本模板,颜色部分是注释
#!/bin/sh        #!/bin/sh 是说明该脚本调用的shell的类型
#chkconfig: 2345 80 05 其中2345是指明服务的运行等级,80表明是系统启动时要启动第80号服务(服务号可以重复)。05表明是系统关闭要停止的服务号。
#descrīption: service-name      这里的service-name你可以随意取名,但必须有
case $1 in     case是个选择语句。$1是个变量,用于指代下面的start,stop等 
start)
写服务启动要执行的命令。
;;
stop)
写服务停止时执行的命令
;;
*)
;;
esac
*****************
#这里写其他情况下执行的内容,可以没有
2. 按照模板,写好启动脚本,使用chkconfig安装脚本
按照模板写好启动脚本,改为777模式,拷贝到/etc/init.d/
然后用chkconfig –add [service-name] #这里service-name是脚本中定义的
如果命令成功执行,则不会有任何提示,此时,在系统/etc/rc.d/的特定运行级目录当中,会有相应的脚本产生,一般以 K 或者 S 和你定义的启动顺序号开头,如 K98httpd
3. Apache自启动脚本实例 
#!/bin/bash
#description:http server
#chkconfig: 235 98 98
case "$1" in
start)
      echo "Starting Apache daemon..."
      /usr/local/apache2/bin/apachectl start
      ;;
stop)
      echo "Stopping Apache daemon..."
      /usr/local/apache2/bin/apachectl stop
      ;;
restart)
      echo "Restarting Apache daemon..."
      /usr/local/apache2/bin/apachectl restart
      ;;
status)
      statusproc /usr/local/apache2/bin/httpd
      ;;
     
*)
      echo "Usage: $0 {start|stop|restart|status}"
      exit 1
      ;;
esac
4. Chkconfig的使用
chkconfig --add service
chkconfig –list     查看,列表中可以看到各种服务在各个运行级的开放情况
chkconfig –delete service-nme