-
Notifications
You must be signed in to change notification settings - Fork 331
/
Bifrost-server
executable file
·152 lines (132 loc) · 3.45 KB
/
Bifrost-server
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
#!/bin/sh
#Copyright [2018] [jc3wish]
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#http://www.apache.org/licenses/LICENSE-2.0
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
bin_dir=
pid_file=
config_file=
dir_dir=
mode=$1 # start or stop or restart
basepath=$(cd `dirname $0`;pwd)
basepathParent=$(dirname $basepath)
__readINI () {
INIFILE=$1; SECTION=$2; ITEM=$3;
_readIni=`cat $INIFILE | awk 'BEGIN{FS="=";OFS=":";}/\'$SECTION'\]/,/\[.*[^('$SECTION')].*\]/{gsub(/[[:blank:]]*/,"",$1);if(NF==2 && $1=="'$ITEM'"){gsub(/^[[:blank:]]*/,"",$2);gsub(/[[:blank:]]*$/,"",$2);print $2;}}'`
echo "${_readIni}"
}
if test -z "$bin_dir"
then
bin_dir="$basepath"
fi
if test -z "$config_file"
then
config_file="$basepathParent/etc/Bifrost.ini"
fi
if test -z "$dir_dir"
then
dir_dir=$(__readINI $config_file Bifrostd data_dir);
fi
if test -z "$dir_dir"
then
dir_dir="$basepathParent/data"
fi
if [[ $dir_dir != /* ]]; then
dir_dir=$basepathParent"/"$dir_dir
fi
if test -z "$pid_file"
then
pid_file=$(__readINI $config_file Bifrostd pid);
fi
if test -z "$pid_file"
then
pid_file="$dir_dir/Bifrost.pid"
fi
case "$mode" in
'start')
# Start daemon
#echo "Starting Bifrost"
if test -x $bin_dir/Bifrost
then
# if in docker , remove $pid_file
if [ -f "/.dockerenv" ];then
rm -f $pid_file
fi
$bin_dir/Bifrost -d -config $config_file -pid $pid_file
else
echo "Couldn't find Bifrost server ($bin_dir/Bifrost)"
exit 1
fi
exit 0
;;
'stop')
# Stop daemon. We use a signal here to avoid having to know the
if test -s "$pid_file"
then
bifrost_pid=`cat "$pid_file"`
if (kill -0 $bifrost_pid 2>/dev/null)
then
echo "Shutting down Bifrost"
kill $bifrost_pid
for i in 1 2 3
do
sleep 1
if !(kill -0 $bifrost_pid 2>/dev/null)
then
echo "server stop success"
exit 0
fi
done
echo "kill $bifrost_pid failed, retry please"
exit 1
else
echo "Bifrost server process #$bifrost_pid is not running!"
rm "$pid_file"
exit 1
fi
exit 0
else
echo "Bifrost server PID file could not be found!"
exit 1
fi
;;
'restart')
# Stop the service and regardless of whether it was
# running or not, start it again.
if ($0 stop); then
$0 start
else
echo "Failed to stop running server, so refusing to try to start."
exit 1
fi
;;
'status')
# First, check to see if pid file exists
if test -s "$pid_file" ; then
bifrost_pid=`cat "$pid_file"`
if kill -0 $bifrost_pid 2>/dev/null ; then
echo "Bifrost running ($bifrost_pid)"
exit 0
else
echo "Bifrost is not running, but PID file exists"
exit 1
fi
else
echo "PID file not exists"
exit 1
fi
;;
*)
# usage
basename=`basename "$0"`
echo "Usage: $basename {start|stop|restart|status} [ Bifrost server options ]"
exit 1
;;
esac
exit 0