Litao OK Blog

A little blog for my life.

CentOS创建Go应用启动脚本

可以基于下面脚本进行修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
# revel-app go/revel daemon
# chkconfig: 345 20 80
# description: revel-app daemon
# processname: revel-app

NAME="revel-app"
PIDFILE=/var/run/$NAME.pid

DAEMON_PATH="/opt/revel-app"
DAEMON="/bin/bash run.sh"
DAEMON_OPTS=""

case "$1" in
start)
    printf "%-50s" "Starting $NAME..."
    if [ -f $PIDFILE ]; then
        echo "Already running? (pid=`cat $PIDFILE`)"
    else
        cd $DAEMON_PATH
        PID=`$DAEMON $DAEMON_OPTS > /dev/null 2>&1 & echo $!`
        echo "$PID" > $PIDFILE
        echo "Ok. (pid=$PID)"
    fi
;;
status)
    printf "%-50s" "Checking $NAME..."
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
            printf "%s\n" "Process dead but pidfile exists."
        else
            echo "Running. (pid=$PID)"
        fi
    else
        printf "%s\n" "Service not running."
    fi
;;
stop)
    printf "%-50s" "Stopping $NAME"
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        pkill -9 -P $PID
        printf "%s\n" "Ok."
        rm -f $PIDFILE
    else
        printf "%s\n" "Already stoppied? $PIDFILE not found."
    fi
;;
restart)
    $0 stop
    sleep 5
    $0 start
;;
*)
    echo "Usage: $0 {status|start|stop|restart}"
    exit 1
esac