-
Notifications
You must be signed in to change notification settings - Fork 313
/
service.sh
89 lines (83 loc) · 1.75 KB
/
service.sh
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
# -------------------------------------
# 服务启动脚本
#
# @author jesse.li
# @date 2016.05.13
# -------------------------------------
workDir=$(cd `dirname $0`; pwd)
binFile="$workDir/gopub"
pidFile="$workDir/gopub.pid"
error=""
cd $workDir
start() {
nohup $binFile > /dev/null 2>&1 &
echo $! > $pidFile
}
stop() {
if [[ -e $pidFile ]]; then
pid=`cat $pidFile`
rm -f $pidFile
else
pid=`ps aux | grep gopub | grep -v grep | awk '{print $2}' | head -1`
fi
if [ "$pid"x != ""x ]; then
kill -9 $pid
else
error="服务不在运行状态"
return 1
fi
}
case $1 in
start)
if [[ -e $pidFile ]]; then
echo "服务正在运行中, 进程ID: " $(cat $pidFile)
exit 1
fi
echo -n "正在启动 ... "
start
sleep 1
echo "成功, 进程ID:" $(cat $pidFile)
;;
stop)
echo -n "正在停止 ... "
stop
if [[ $? -gt 0 ]]; then
echo "失败, ${error}"
else
echo "成功"
fi
;;
restart)
echo -n "正在重启 ... "
stop
sleep 1
start
echo "成功, 进程ID:" $(cat $pidFile)
;;
status)
if [[ -e $pidFile ]]; then
pid=$(cat $pidFile)
else
pid=`ps aux | grep gopub | grep -v grep | awk '{print $2}' | head -1`
fi
if [[ -z "$pid" ]]; then
echo "服务不在运行状态"
exit 1
fi
exists=$(ps -ef | grep $pid | grep -v grep | wc -l)
if [[ $exists -gt 0 ]]; then
echo "服务正在运行中, 进程ID为${pid}"
else
echo "服务不在运行状态, 但进程ID文件存在"
fi
;;
build)
rebuild
;;
*)
echo "GoPub启动脚本"
echo "用法: "
echo " ./service.sh (start|stop|restart|status)"
;;
esac