小试牛刀 chkconfig设置自定义服务随机启动
:
[root@xencms2 ~]# vim /etc/init.d/convirtd #注意脚本的存放位置
#! /bin/sh
#Author QQ:335623998
#chkconfig: 2345 66 66 #2345是定义该服务的启动级别,第一个66是启动序号,第二个66是关闭序号
#description:Convirt #服务描述
#/etc/init.d/convirtd
# Source function library.
. /etc/rc.d/init.d/functions
pidfile=${PIDFILE-/root/convirt/paster.pid}
convirtd=${CONVIRTD-/root/convirt/convirt-ctl}
. /etc/rc.d/init.d/functions
pidfile=${PIDFILE-/root/convirt/paster.pid}
convirtd=${CONVIRTD-/root/convirt/convirt-ctl}
case "$1" in
start)
echo "Starting Convirt daemon"
su - root -c "cd /root/convirt/ && ./convirt-ctl start &>/dev/null"
;;
stop)
echo "Shutting down Convirt daemon"
su - root -c "cd /root/convirt/ && ./convirt-ctl stop &>/dev/null"
;;
status)
status -p ${pidfile} $convirtd
RETVAL=$?
;;
status -p ${pidfile} $convirtd
RETVAL=$?
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac
"/etc/rc.d/init.d/convirtd" 22L, 511C written
[root@xencms2 ~]# chkconfig --add convirtd #将服务加入chkconfig管理
[root@xencms2 ~]# chkconfig --list convirtd
convirtd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
确认脚本启动、关闭是否符合要求:
1、查看chkconfig管理服务启动级别
[root@xencms2 ~]# ll /etc/rc3.d/S66convirtd
lrwxrwxrwx 1 root root 18 May 21 12:33 /etc/rc3.d/S66convirtd -> ../init.d/convirtd
[root@xencms2 ~]# ll /etc/rc1.d/K66convirtd
lrwxrwxrwx 1 root root 18 May 21 12:33 /etc/rc1.d/K66convirtd -> ../init.d/convirtd
2、脚本关闭服务功能
[root@xencms2 convirt]# ps aux|grep convirt |grep -v grep
root 15590 12.3 0.4 714472 56312 ? Sl 13:57 0:02 /root/convirt/tg2env/bin/python /root/convirt/tg2env/bin/paster serve ./src/convirt/web/convirt/development.ini --daemon --pid-file ./paster.pid
#已经有进程,说明服务是启动状态
[root@xencms2 convirt]# /etc/init.d/convirtd stop #测试关闭功能
Shutting down Convirt daemon
[root@xencms2 convirt]# ps aux|grep convirt |grep -v grep #查看已经无进程
3、脚本查看服务状态功能
[root@xencms2 convirt]# /etc/init.d/convirtd status
convirt-ctl is stopped
4、脚本启动服务功能
[root@xencms2 convirt]# /etc/init.d/convirtd start
Starting Convirt daemon
[root@xencms2 convirt]# ps aux|grep convirt |grep -v gre
root 15764 46.2 0.4 778332 54364 ? Sl 13:58 0:02 /root/convirt/tg2env/bin/python /root/convirt/tg2env/bin/paster serve ./src/convirt/web/convirt/development.ini --daemon --pid-file ./paster.pid
[root@xencms2 convirt]# /etc/init.d/convirtd status
convirt-ctl (pid 15764) is running...
本文转自pandazhai 51CTO博客,原文链接:http://blog.51cto.com/dreamway/1045554