Skip to content

Commit

Permalink
update init script
Browse files Browse the repository at this point in the history
  • Loading branch information
teddysun committed Sep 17, 2016
1 parent ff10db9 commit 860ad4b
Showing 1 changed file with 69 additions and 71 deletions.
140 changes: 69 additions & 71 deletions shadowsocks-libev-debian
Original file line number Diff line number Diff line change
@@ -1,108 +1,106 @@
#!/bin/bash
# Start/stop shadowsocks.
#


### BEGIN INIT INFO
# Provides: shadowsocks-libev
# Provides: Shadowsocks-libev
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $remote_fs
# Should-Start:
# Should-Stop:
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: shadowsocks-libev 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

# Author: Teddysun <[email protected]>

# Daemon
NAME=shadowsocks-libev
BIN=/usr/local/bin/ss-server
CONFIG_FILE=/etc/shadowsocks-libev/config.json
if [ -f /usr/local/bin/ss-server ]; then
DAEMON=/usr/local/bin/ss-server
elif [ -f /usr/bin/ss-server ]; then
DAEMON=/usr/bin/ss-server
fi
NAME=Shadowsocks-libev
CONF=/etc/shadowsocks-libev/config.json
PID_DIR=/var/run
PID_FILE=$PID_DIR/shadowsocks.pid
PID_FILE=$PID_DIR/shadowsocks-libev.pid
RET_VAL=0

[ -x $BIN ] || exit 0
[ -x $DAEMON ] || exit 0

if [ ! -d $PID_DIR ]; then
mkdir -p $PID_DIR
if [ $? -ne 0 ]; then
echo "Creating PID directory $PID_DIR failed"
exit 1
fi
fi

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
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
rm -f $PID_FILE
return 1
return 2
fi
else
return 2
fi
}

do_status() {
check_running
case $? in
0)
echo "$NAME running with PID $PID"
;;
1)
echo "$NAME not running, remove PID file $PID_FILE"
;;
2)
echo "Could not find PID file $PID_FILE, $NAME does not appear to be running"
;;
esac
return 0
check_running
case $? in
0)
echo "$NAME (pid $PID) is running..."
;;
1|2)
echo "$NAME is stopped"
RET_VAL=1
;;
esac
}

do_start() {
if [[ ! -d $PID_DIR ]]; then
mkdir -p $PID_DIR || echo "failed creating PID directory $PID_DIR"; exit 1
fi
if check_running; then
echo "$NAME already running with PID $PID"
return 0
fi
if [[ ! -r $CONFIG_FILE ]]; then
echo "config file $CONFIG_FILE not found"
return 1
fi
echo "starting $NAME"
# sudo will set the group to the primary group of $USER
$BIN -u -c $CONFIG_FILE > /dev/null 2>&1 &
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
if check_running; then
echo "$NAME (pid $PID) is already running..."
return 0
fi
$DAEMON -u -c $CONF -f $PID_FILE
if check_running; then
echo "Starting $NAME success"
else
echo "Starting $NAME failed"
RET_VAL=1
fi
}

do_stop() {
if check_running; then
echo "stopping $NAME with PID $PID"
kill $PID
rm -f $PID_FILE
else
echo "Could not find PID file $PID_FILE"
fi
if check_running; then
kill -9 $PID
rm -f $PID_FILE
echo "Stopping $NAME success"
else
echo "$NAME is stopped"
RET_VAL=1
fi
}

do_restart() {
do_stop
do_start
do_stop
do_start
}

case "$1" in
start|stop|restart|status)
start|stop|restart|status)
do_$1
;;
*)
echo "Usage: shadowsocks {start|stop|restart|status}"
*)
echo "Usage: $0 { start | stop | restart | status }"
RET_VAL=1
;;
esac
Expand Down

0 comments on commit 860ad4b

Please sign in to comment.