-
Notifications
You must be signed in to change notification settings - Fork 5
/
init.sh
executable file
·154 lines (129 loc) · 2.84 KB
/
init.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/sh
#
# transport - this script start and stop the transport daemon
#
# chkconfig:
# description:
#
# process name: transport
# config: config.json
# pidfile: var/transport.pid
# Source env
#. /etc/profile
# Source function library.
#. /etc/rc.d/init.d/functions
# Source networking configuration.
#. /etc/sysconfig/network
NOW=$(date +%Y-%m-%d.%H:%M:%S)
echo $NOW
DIR=$(pwd) #当前目录
BASEDIR=$(dirname $(dirname $(dirname $DIR)))
export GOPATH=$GOPATH:$BASEDIR
export GOBIN=$GOPATH/bin/
echo "GOPATH init Finished. GOPATH=$GOPATH"
#################################
APP=$(basename $DIR)
######创建程序运行临时文件#######
mkdir -p $DIR/var
PIDFile=$DIR/var/$APP.pid
LOGFile=$DIR/var/$APP.log
CONFIG=config.json
#编译$2,默认$2未指定时编译main.go
if [ -z $2 ];then #如果$2为空
main="main.go"
else
main=$2
fi
#main="cmd/transport/main"
################################
function check_PID() {
if [ -f $PIDFile ];then
PID=$(cat $PIDFile)
if [ -n $PID ]; then
running=$(ps -p $PID|grep -v "PID TTY" |wc -l)
return $running
fi
fi
return 0
}
function build() {
gofmt -w .
go build -o $APP $1
if [ $? -ne 0 ]; then
exit $?
fi
echo "build $APP success."
}
function start() {
check_PID
local running=$?
if [ $running -gt 0 ];then
echo -n "$APP now is running already, PID="
cat $PIDFile
return 1
fi
nohup ./$APP -f $CONFIG >$LOGFile 2>&1 &
sleep 1
running=`ps -p $! | grep -v "PID TTY" | wc -l`
if [ $running -gt 0 ];then
echo $! > $PIDFile
echo "$APP started..., PID=$!"
else
echo "$APP failed to start!!!"
return 1
fi
}
function status() {
ps -ef |grep $APP|grep -v grep
check_PID
local running=$?
if [ $running -gt 0 ];then
echo -n "$APP now is running already, PID="
cat $PIDFile
return 1
else
echo "$APP is stopped..."
return 1
fi
}
function debug() {
go run $1
}
function stop() {
local PID=$(cat $PIDFile)
kill $PID
rm -f $PIDFile
echo "$APP stoped..."
}
function restart() {
stop
sleep 1
start
}
function pack() {
tar -cvf $APP.tar.gz $APP config.json init.sh
}
function tailf() {
tail -f $LOGFile
}
function help() {
echo "$0 build|start|stop|kill|restart|reload|run|tail|docs|sslkey"
}
case "$1" in
build) build $main ;;
install) mv $APP /usr/local/bin;;
start) start ;;
debug) debug $main ;;
stop) stop ;;
kill) killall ;;
restart) restart ;;
reload) reload ;;
status) status ;;
run) run ;;
pack) pack ;;
tail) tailf ;;
docs) docs ;;
sslkey) sslkey ;;
*) help ;;
esac
#bee api cmdb-api -driver=mysql -conn="root:root888@tcp(127.0.0.1:3306)/cmdb"