update init script

This commit is contained in:
Teddysun 2016-09-16 18:20:07 +09:00 committed by GitHub
parent 56af963fcf
commit 9d5e612dd7

View File

@ -1,106 +1,105 @@
#!/bin/bash #!/bin/bash
# Start/stop shadowsocks.
#
### BEGIN INIT INFO ### BEGIN INIT INFO
# Provides: shadowsocks-go # Provides: Shadowsocks-go
# Required-Start: $network $local_fs $remote_fs # Required-Start: $network $local_fs $remote_fs
# Required-Stop: $remote_fs # Required-Stop: $network $local_fs $remote_fs
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5 # Default-Start: 2 3 4 5
# Default-Stop: 0 1 6 # Default-Stop: 0 1 6
# Short-Description: shadowsocks is a lightweight tunneling proxy # Short-Description: Fast tunnel proxy that helps you bypass firewalls
# Description: A secure socks5 proxy, designed to protect your Internet traffic.
### END INIT INFO ### END INIT INFO
# Author: Teddysun <i@teddysun.com> # Author: Teddysun <i@teddysun.com>
# Daemon NAME=Shadowsocks-go
NAME=shadowsocks-go
BIN=/usr/bin/shadowsocks-server BIN=/usr/bin/shadowsocks-server
CONFIG_FILE=/etc/shadowsocks/config.json CONF=/etc/shadowsocks/config.json
PID_DIR=/var/run PID_DIR=/var/run
PID_FILE=$PID_DIR/shadowsocks.pid PID_FILE=$PID_DIR/shadowsocks-go.pid
RET_VAL=0 RET_VAL=0
[ -x $BIN ] || exit 0 [ -x $BIN ] || exit 0
check_running() { if [[ ! -d $PID_DIR ]]; then
if [[ -r $PID_FILE ]]; then mkdir -p $PID_DIR
read PID <$PID_FILE if [ $? -ne 0 ]; then
if [[ -d "/proc/$PID" ]]; then echo "Creating PID directory $PID_DIR failed"
return 0 RET_VAL=1
else fi
rm -f $PID_FILE fi
return 1
if [ ! -f $CONF ]; then
echo "$NAME config file $CONF not found"
exit 1
fi
check_running() {
if [[ -r $PID_FILE ]]; then
read PID < $PID_FILE
if [[ -d "/proc/$PID" ]]; then
return 0
else
rm -f $PID_FILE
return 1
fi
else
return 2
fi fi
else
return 2
fi
} }
do_status() { do_status() {
check_running check_running
case $? in case $? in
0) 0)
echo "$NAME running with PID $PID" echo "$NAME is running with PID $PID"
;; ;;
1) 1|2)
echo "$NAME not running, remove PID file $PID_FILE" echo "$NAME is not running"
;; RET_VAL=1
2) ;;
echo "Could not find PID file $PID_FILE, $NAME does not appear to be running" esac
;;
esac
return 0
} }
do_start() { do_start() {
if [[ ! -d $PID_DIR ]]; then if check_running; then
mkdir $PID_DIR || echo "failed creating PID directory $PID_DIR"; exit 1 echo "$NAME is already running with PID $PID"
fi return 0
if check_running; then fi
echo "$NAME already running with PID $PID" $BIN -c $CONF 2>&1 > /dev/null &
return 0 PID=$!
fi echo $PID > $PID_FILE
if [[ ! -r $CONFIG_FILE ]]; then sleep 0.3
echo "config file $CONFIG_FILE not found" if check_running; then
return 1 echo "$NAME start success"
fi else
echo "starting $NAME" echo "$NAME start failed"
# sudo will set the group to the primary group of $USER RET_VAL=1
$BIN -c $CONFIG_FILE > /dev/null & fi
PID=$!
echo $PID > $PID_FILE
sleep 0.3
if ! check_running; then
echo "start failed"
return 1
fi
echo "$NAME running with PID $PID"
return 0
} }
do_stop() { do_stop() {
if check_running; then if check_running; then
echo "stopping $NAME with PID $PID" kill -9 $PID
kill $PID rm -f $PID_FILE
rm -f $PID_FILE echo "$NAME stop success"
else else
echo "Could not find PID file $PID_FILE" echo "$NAME is not running"
fi RET_VAL=1
fi
} }
do_restart() { do_restart() {
do_stop do_stop
do_start do_start
} }
case "$1" in case "$1" in
start|stop|restart|status) start|stop|restart|status)
do_$1 do_$1
;; ;;
*) *)
echo "Usage: shadowsocks {start|stop|restart|status}" echo "Usage: $0 { start | stop | restart | status }"
RET_VAL=1 RET_VAL=1
;; ;;
esac esac