linux /etc/init.d/ 服务脚本

时间:2022-03-11 04:29:39

1. 脚本写好后放入到 /etc/init.d/目录下,并设置可执行权限chmod +x scriptnamed

2. 脚本中包含start,stop,restart,status几种方法,

/opt/myservice/myservice.sh脚本可以添加你自己想要执行的操作

例子如下:

#!/bin/bash
#chkconfig:1234 90 60
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# description: Saves and restores system entropy pool for \
# higher quality random number generation
servername=myservice
serverdir=/opt/myservice
binpath=/opt/myservice/myservice.sh

prog=$(basename $binpath)
. /etc/init.d/functions

restart() {
        stop
        start
}
reload() {
        stop
        start
}
start() {
echo -n $"Starting $daemon:"
        daemon $binpath start
        retval=$?
        echo
        [ $retval -eq 0 ]
}

stop() {
echo -n $"Stopping $daemon:"
        daemon $binpath stop
        retval=$?
        echo
        [ $retval -eq 0 ]
}

ha_status() {
        #status $prog
        status $prog
        ps -ef|grep $prog && exit 0
}

case "$1" in

     start)
        $1
     ;;
     stop)
        $1
     ;;
     reload)
        $1
     ;;
     restart)
        $1
     ;;
     status)
        ha_status
     ;;
     *)
        echo "Usage:$0 {start|stop|reload|restart|status}"
        exit 1
esac


3.最后设置为开机启动设置chkconfig --level 2345 scriptname on