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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章