Skip to content

Commit

Permalink
Enhance Linux startup script.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Jan 24, 2013
1 parent bfe063b commit 88cbccd
Showing 1 changed file with 83 additions and 55 deletions.
138 changes: 83 additions & 55 deletions sample-config/shadowsocks
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,92 @@ CONFIG_FILE=/etc/shadowsocks/config.json
LOG_FILE=/var/log/shadowsocks
USER=nobody
GROUP=nobody
PID_DIR=/var/run/
PID_DIR=/var/run
PID_FILE=$PID_DIR/shadowsocks.pid
RET_VAL=0

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
}

do_status() {
check_running
case $? in
0)
echo "shadowsocks running with PID $PID"
;;
1)
echo "shadowsocks not running, remove PID file $PID_FILE"
;;
2)
echo "Could not find PID file $PID_FILE, shadowsocks does not appear to be running"
;;
esac
return 0
}

do_start() {
if [[ ! -d $PID_DIR ]]; then
echo "creating PID dir"
mkdir $PID_DIR || echo "failed creating PID directory $PID_DIR"; exit 1
chown $USER:$GROUP $PID_DIR || echo "failed creating PID directory $PID_DIR"; exit 1
chmod 0770 $PID_DIR
fi
if check_running; then
echo "shadowsocks 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 shadowsocks"
# sudo will set the group to the primary group of $USER
sudo -u $USER $BIN -c $CONFIG_FILE >>$LOG_FILE &
PID=$!
echo $PID > $PID_FILE
sleep 0.3
if ! check_running; then
echo "start failed, log file $LOG_FILE may provide some help"
return 1
fi
echo "shadowsocks running with PID $PID"
return 0
}

do_stop() {
if check_running; then
echo "stopping shadowsocks with PID $PID"
kill $PID
rm -f $PID_FILE
else
echo "Could not find PID file $PID_FILE"
fi
}

do_restart() {
do_stop
do_start
}

case "$1" in
start)
if [[ ! -d $PID_DIR ]]
then
mkdir $PID_DIR
chown $USER:$GROUP $PID_DIR
chmod 0770 $PID_DIR
fi
if [[ -r $PID_FILE ]]
then
echo "shadowsocks already running with PID `cat $PID_FILE`"
RET_VAL=1
else
# sudo will set the group to the primary group of $USER
sudo -u $USER $BIN -c $CONFIG_FILE >$LOG_FILE &
echo $! > $PID_FILE
RET_VAL=$?
fi
;;
stop)
if [[ -r $PID_FILE ]]
then
kill `cat $PID_FILE`
rm $PID_FILE
RET_VAL=$?
else
echo "Could not find PID file $PID_FILE"
RET_VAL=1
fi
;;
restart)
if [[ -r $PID_FILE ]]
then
kill `cat $PID_FILE`
rm $PID_FILE
RET_VAL=$?
else
echo "Could not find PID file $PID_FILE"
fi
start
RET_VAL=$?
;;
status)
if [[ -r $PID_FILE ]]
then
echo "shadowsocks running with PID `cat $PID_FILE`"
RET_VAL=$?
else
echo "Could not find PID file $PID_FILE, shadowsocks does not appear to be running"
fi
;;
*)
echo "Usage: shadowsocks {start|stop|restart|status}"
RET_VAL=1
;;
start|stop|restart|status)
do_$1
;;
*)
echo "Usage: shadowsocks {start|stop|restart|status}"
RET_VAL=1
;;
esac

exit $RET_VAL

0 comments on commit 88cbccd

Please sign in to comment.