Commit 1e20cff1740d11f9cf6b073ef9904d3996054f9b

Authored by Clang
Committed by GitHub
1 parent c14a007b
Exists in master

Update ngrokd.init

Showing 1 changed file with 61 additions and 57 deletions   Show diff stats
ngrokd.init
... ... @@ -18,7 +18,12 @@
18 18 # Short-Description: starts the ngrok
19 19 # Description: starts ngrok using start-stop
20 20 ### END INIT INFO
21   -
  21 +# Source function library
  22 +if [ -f /etc/rc.d/init.d/functions ]; then
  23 + . /etc/rc.d/init.d/functions
  24 +# Check that networking is up.
  25 + [ ${NETWORKING} ="yes" ] || exit 0
  26 +fi
22 27 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
23 28 ProgramName="Ngrok"
24 29 ProGramInstallPath="/usr/local/ngrok"
... ... @@ -29,13 +34,19 @@ CONFIGFILE=${ProGramInstallPath}/.ngrok_config.sh
29 34 LOGFILE=${ProGramInstallPath}/ngrok.log
30 35 SCRIPTNAME=/etc/init.d/${NAME}
31 36 PID_DIR=/var/run
32   -PID_FILE=$PID_DIR/ngrok_clang.pid
33   -version="7.0"
  37 +PID_FILE=${PID_DIR}/ngrok_clang.pid
  38 +version="8.0"
34 39 manage_port="4446"
35 40 RET_VAL=0
36 41  
37 42 [ -x $BIN ] || exit 0
38   -
  43 +if [ ! -d $PID_DIR ]; then
  44 + mkdir -p $PID_DIR
  45 + if [ $? -ne 0 ]; then
  46 + echo "Creating PID directory $PID_DIR failed"
  47 + exit 1
  48 + fi
  49 +fi
39 50 fun_clangcn()
40 51 {
41 52 echo ""
... ... @@ -46,16 +57,15 @@ fun_clangcn()
46 57 echo "+----------------------------------------------+"
47 58 echo ""
48 59 }
49   -
50   -fun_check_run()
51   -{
52   - if netstat -tnpl | grep -q ${NAME};then
  60 +fun_check_run() {
  61 + PID=`ps -ef | grep -v grep | grep -i "${BIN}" | awk '{print $2}'`
  62 + if [ ! -z $PID ]; then
53 63 return 0
54 64 else
55   - rm -f $PID_FILE
56 65 return 1
57 66 fi
58 67 }
  68 +
59 69 fun_load_config(){
60 70 if [ ! -r ${CONFIGFILE} ]; then
61 71 echo "config file ${CONFIGFILE} not found"
... ... @@ -108,25 +118,28 @@ fun_start()
108 118 if [ ! -d $PID_DIR ]; then
109 119 mkdir -p $PID_DIR || echo "failed creating PID directory ${PID_DIR}"; exit 1
110 120 fi
111   - if fun_check_run; then
112   - echo "${ProgramName} (pid `pidof $NAME`) already running."
113   - return 0
  121 + if [ -f ${PID_FILE} ]; then
  122 + read PID < ${PID_FILE}
  123 + echo "$NAME (pid $PID) is already running..."
  124 + exit 0
114 125 fi
115   - echo -n "Starting ${ProgramName}..."
  126 + echo -n "Starting ${ProgramName}: "
116 127 fun_check_port
117 128 fun_load_config
118 129 ${BIN} -domain="$dns" -httpAddr=":$http_port" -httpsAddr=":$https_port" -pass="$pass" -tlsCrt="$srtCRT" -tlsKey="$strKey" -tunnelAddr=":$remote_port"${log_level} > ${LOGFILE} 2>&1 &
119   - PID=`pidof ${NAME}`
120   - echo $PID > $PID_FILE
121   - sleep 0.3
122   - if ! fun_check_run; then
123   - echo "start failed"
124   - return 1
  130 + RETVAL=$?
  131 + PID=$(ps -ef | grep -v grep | grep -i ${BIN} | awk '{print $2}')
  132 + echo ${PID} > ${PID_FILE}
  133 + if [ -f /etc/rc.d/init.d/functions ]; then
  134 + [ $RETVAL -eq 0 ] && success
  135 + else
  136 + if fun_check_run; then
  137 + echo " [ok]"
  138 + else
  139 + echo " [FAILED]"
  140 + RETVAL=1
  141 + fi
125 142 fi
126   - echo " done"
127   - echo "${ProgramName} (pid `pidof $NAME`)is running."
128   - echo "read ${LOGFILE} for log"
129   - return 0
130 143 }
131 144  
132 145 fun_stop(){
... ... @@ -134,32 +147,41 @@ fun_stop(){
134 147 fun_clangcn
135 148 fi
136 149 if fun_check_run; then
137   - echo -n "Stoping ${ProgramName} (pid `pidof $NAME`)... "
138   - kill `pidof $NAME`
139   - if [ "$?" != 0 ] ; then
140   - echo " failed"
141   - exit 1
  150 + echo -n $"Stopping ${ProgramName}: "
  151 + if [ -f /etc/rc.d/init.d/functions ]; then
  152 + killproc -p $PID_FILE >/dev/null 2>&1
142 153 else
143   - echo " done"
144   - rm -f $PID_FILE
  154 + if fun_check_run; then
  155 + kill -9 $PID >/dev/null 2>&1
  156 + echo " [ok]"
  157 + else
  158 + echo " [FAILED]"
  159 + RETVAL=1
  160 + fi
145 161 fi
  162 + RETVAL=$?
  163 + [ $RETVAL -eq 0 ]
  164 + rm -f $PID_FILE
146 165 else
147 166 echo "${ProgramName} is not running."
148 167 fi
149 168 }
  169 +
150 170 fun_restart(){
151 171 fun_stop
152 172 fun_start
153 173 }
154 174 fun_status(){
155   - if netstat -tnpl | grep -q ${NAME}; then
156   - PID=`pidof ${NAME}`
157   - echo "${ProgramName} (pid $PID) is running..."
158   - netstat -tnpl | grep "${NAME}"
159   - else
160   - echo "${ProgramName} is stopped"
161   - exit 0
162   - fi
  175 + fun_check_run
  176 + case $? in
  177 + 0)
  178 + echo "$NAME (pid $PID) is running..."
  179 + ;;
  180 + 1|2)
  181 + echo "$NAME is stopped"
  182 + RET_VAL=1
  183 + ;;
  184 + esac
163 185 }
164 186 checkos(){
165 187 if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
... ... @@ -173,27 +195,9 @@ checkos(){
173 195 exit 1
174 196 fi
175 197 }
176   -check_nano(){
177   - nano -V >/dev/null
178   - if [[ $? -gt 1 ]] ;then
179   - echo " Run nano failed"
180   - checkos
181   - if [ "${OS}" == 'CentOS' ]; then
182   - echo " Install centos nano ..."
183   - #yum -y update
184   - yum -y install nano
185   - else
186   - echo " Install debian/ubuntu nano ..."
187   - apt-get update -y
188   - apt-get install -y nano
189   - fi
190   - fi
191   - echo $result
192   -}
193 198 fun_config(){
194   - check_nano
195 199 if [ -s ${CONFIGFILE} ]; then
196   - nano ${CONFIGFILE}
  200 + vi ${CONFIGFILE}
197 201 else
198 202 echo "${ProgramName} configuration file not found!"
199 203 fi
... ...