service脚本的写法

/etc/init.d/appservice.sh内容如下:
#!/bin/bash
# chkconfig: 2345 20 80
# description:
# For SELinux we need to use 'runuser' not 'su'
if [ -x "/sbin/runuser" ]; then
    SU="/sbin/runuser -s /bin/sh"
else
    SU="su -s /bin/sh"
fi
fileroot=/opt/cmbccd/repost/
#program=${fileroot}"start-appservice.sh"
pidfile=${fileroot}"repost.pid"
case "$1" in
start)
    echo "starting repost..."
        if [ -f $pidfile ];
    then
        echo "service is running!"
        echo "[fail]"
    else
        #sh $program
        $SU kafka -c ${fileroot}"start-appservice.sh"
        echo "[success]"
        fi
    ;;
stop)
    if [ -f $pidfile ];
    then
        pid=$(cat $pidfile)
        echo "stopping service,the pid is:" $pid
        echo $(kill -9 $pid)
        echo $(rm -r $pidfile)
        echo "[success]"
    else
        echo "service is stopped!"
        echo "[fail]"
    fi
    ;;
status)
    if [ -f $pidfile ];
    then
        pid=$(cat $pidfile)
        echo "service is running,pid is:" $pid
    else
        echo "service is stopped!"
    fi
    ;;
*)
    echo "Usage: $0 {start|stop|status}"
    exit 1
esac

posted on 2017-04-21 16:41  石下江的太阳光  阅读(283)  评论(0编辑  收藏  举报

导航