redhat ,centos 自定义service 脚本,并能用chkconfig管理

之前一篇写了自定义的serivce脚本,可以用service myserivce start|stop|restart|status来使用。

但是不能用chkconfig来管理和启动成功无标记的颜色。


因为将service加到chkconfig管理的时候,用的方法是#chkconfig --add service名字


所以,我man了一下chkconfig的用法,发现一个有趣的地方:


--add name


             This option adds a new  service  for  management  by  chkconfig.

             When  a new service is added, chkconfig ensures that the service

             has either a start or a kill entry in  every  runlevel.  If  any

             runlevel  is missing such an entry, chkconfig creates the appro-

             priate entry as specified by the  default  values  in  the  init

             script.  Note  that default entries in LSB-delimited ’INIT INFO’

             sections take precedence  over  the  default  runlevels  in  the

             initscript;  if  any Required-Start or Required-Stop entries are

             present, the start and stop priorities of  the  script  will  be

             adjusted to account for these dependencies.


和下面的LSB-delimited 的部分:

wKiom1M6V_Wg551SAAJVzYXKC0I967.jpg


由此看来,要在myservice添加这样的一段东西,myservice才能被添加到chkconfig管理。所以我查看了一下/etc/init.d/vsftpd的脚本,发现也有类型的一段文字.

好吧,现在动手修改的我自定义service , 如下


#!/bin/bash
# chkconfig: 2345 55 25
### BEGIN INIT INFO
# Provides: paylm
# Required-Start: bar
# Defalt-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: myservice init script
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
RETVAL=0
uid=`id -u`
# it must be add empty file in /var/lock/subsys/
lockfile='/var/lock/subsys/myservice'
function myStart(){
#       local lockfile='/var/lock/subsys/myservice'
#       [ -f $lockfile ] && rm -rf $lockfile || touch $lockfile
        if [ ! -f $lockfile ];then
                touch $lockfile
                echo -n "start myservice :.."
                sleep 1
                echo -e "\t\t [ \e[0;32;1mOK\e[0m ]"
        else
                logger "myservice is running ... , please stop it and start again "
                echo -n "start myservice :.."
                sleep 1
                echo -e "\t\t [ \e[0;32;1mFail\e[0m ]"
        fi
        RETVAL=0
}
function myStop(){
#       local lockfile='/var/lock/subsys/myservice'
        [ -f $lockfile ] && rm -rf $lockfile
        echo -n "stop myservice :"
        echo -e "\t\t [ \e[0;32;1mOK\e[0m ]"
        RETVAL=1
}
case "$1" in
        start)
                [ $uid -eq 0 ] && myStart || exit 6
                ;;
        stop)
                [ $uid -eq 0 ] && myStop || exit 7
                ;;
        status)
                if [ $uid -eq 0 -a -f $lockfile ];then
                        echo -e "myservice is running .."
                   else
                        echo -e "myservice is not running.."
                fi
                ;;
        restart)
                [ $uid -eq 0 ] && myStop && myStart
                ;;
        *)
                echo -e "Usage: $0 {start|stop|restart|status}
"
                exit 1 ;
                ;;
esac

chmod a+x myservice

运行结果显示图:


wKioL1M6WoKCR3Q9AAIEVLfrkuU351.jpg

你可能感兴趣的:(redhat ,centos 自定义service 脚本,并能用chkconfig管理)