跳转至

配置一个启停脚本

Bash
#!/usr/bin/env bash
# Auther: chaic
# Description: smokeping service
#
# Simple smokeping init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

### BEGIN INIT INFO
# Provides:     smokeping
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    smokeping service
# Description:          smokeping service. See chaic wiki
### END INIT INFO

EXEC=/usr/local/smokeping/bin/smokeping
LOGFILE=/var/log/smokeping.log
CONF=/usr/local/smokeping/etc/config
PIDFILE=/var/run/smokeping.pid

checkpid(){
    pid=`ps -ef |grep $EXEC |awk '$3 == 1'  |awk '{print $2}'`
    echo $pid > $PIDFILE
}

start(){
    checkpid
    if [ ! -n "$pid" ]; then
        echo "Starting Smokeping server..."
        $EXEC --logfile=$LOGFILE
        checkpid
        echo -e "\033[32m >>>>>> The Smokeping service is start: PID :${pid} \033[0m"
    else
        echo -e "\033[32m Smokeping starting...\033[0m"
    fi
}
stop(){
    checkpid
    if [ ! -n "$pid" ]; then
     echo -e "\033[41m >>>>>> Smokeping not runing \033[0m"
    else
      echo -e "\033[33m Smokeping stopping...\033[0m"
      kill $pid
      echo -e "\033[33m >>>>>> kill $pid\033[0m"
      sleep 3
      checkpid
      if [ -n "$pid" ]; then
      kill -9 $pid
      echo -e "\033[33m >>>>>> kill -9 $pid\033[0m"
      fi
    fi
}

status(){
    checkpid
    if [ ! -n "$pid" ]; then
      echo -e "\033[41m >>>>>> Smokeping not running \033[0m"
    else
      echo -e "\033[32m >>>>>> Smokeping running PID: $pid \033[0m"
    fi
}
restart(){
    echo -e "\033[32m Smokeping restart..\033[0m"
    $EXEC --restart --logfile=$LOGFILE
    checkpid
    echo -e "\033[32m >>>>>> The Smokeping service is restart: PID :${pid} \033[0m"
}
reload(){
    checkpid
    if [ ! -n $pid ]
    then
        echo -e "\033[41m Smokeping not runing...\033[0m"
    else
        echo "Reload Smokeping server..."
        $EXEC --reload --logfile=$LOGFILE
        checkpid
        echo -e "\033[32m >>>>>> The Smokeping service is Reload: PID :${pid} \033[0m"
    fi
}

case "$1" in
    start)start;;
    stop)stop;;
    restart)restart;;
    reload)reload;;
    status)status;;
    *)
        echo "Please use [ start | stop | restart | reload | status ] as first argument"
        ;;
esac