forked from teddysun/shadowsocks_install
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
88 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,109 @@ | ||
#!/bin/bash | ||
# | ||
# Script to run Shadowsocks in daemon mode at boot time. | ||
# Revision 1.0 | ||
|
||
### BEGIN INIT INFO | ||
# Provides: shadowsocks | ||
# Required-Start: | ||
# Required-Stop: | ||
# Provides: shadowsocks-libev | ||
# Required-Start: $network $local_fs $remote_fs | ||
# Required-Stop: $remote_fs | ||
# Should-Start: | ||
# Should-Stop: | ||
# Default-Start: 2 3 4 5 | ||
# Default-Stop: 0 1 6 | ||
# Short-Description: shadowsocks is a lightweight tunneling proxy | ||
# Short-Description: shadowsocks-libev is a lightweight tunneling proxy | ||
### END INIT INFO | ||
|
||
# Daemon | ||
NAME=shadowsocks-server | ||
DAEMON=/usr/local/bin/ss-server | ||
|
||
# Path to the configuration file. | ||
# | ||
CONF=/etc/shadowsocks-libev/config.json | ||
|
||
#USER="nobody" | ||
#GROUP="nobody" | ||
|
||
# Take care of pidfile permissions | ||
mkdir /var/run/$NAME 2>/dev/null || true | ||
#chown "$USER:$GROUP" /var/run/$NAME | ||
# Author: Teddysun <[email protected]> | ||
|
||
# Check the configuration file exists. | ||
# | ||
if [ ! -f $CONF ] ; then | ||
echo "The configuration file cannot be found!" | ||
exit 0 | ||
fi | ||
|
||
# Path to the lock file. | ||
# | ||
LOCK_FILE=/var/lock/subsys/shadowsocks | ||
|
||
# Path to the pid file. | ||
# | ||
PID=/var/run/$NAME/pid | ||
|
||
|
||
#==================================================================== | ||
|
||
#==================================================================== | ||
# Run controls: | ||
|
||
RETVAL=0 | ||
# Daemon | ||
NAME=shadowsocks-libev | ||
BIN=/usr/local/bin/ss-server | ||
CONFIG_FILE=/etc/shadowsocks-libev/config.json | ||
PID_DIR=/var/run/$NAME | ||
PID_FILE=$PID_DIR/pid | ||
RET_VAL=0 | ||
|
||
[ -x $BIN ] || exit 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 | ||
} | ||
|
||
# Start shadowsocks as daemon. | ||
# | ||
start() { | ||
if [ -f $LOCK_FILE ]; then | ||
echo "$NAME is already running!" | ||
exit 0 | ||
else | ||
echo -n $"Starting ${NAME}: " | ||
#daemon --check $DAEMON --user $USER "$DAEMON -f $PID -c $CONF > /dev/null" | ||
daemon $DAEMON -u -c $CONF -f $PID | ||
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 | ||
} | ||
|
||
RETVAL=$? | ||
[ $RETVAL -eq 0 ] && success | ||
echo | ||
[ $RETVAL -eq 0 ] && touch $LOCK_FILE | ||
return $RETVAL | ||
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 -c $CONFIG_FILE & | ||
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() { | ||
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 | ||
} | ||
|
||
# Stop shadowsocks. | ||
# | ||
stop() { | ||
echo -n $"Shutting down ${NAME}: " | ||
killproc -p ${PID} | ||
RETVAL=$? | ||
[ $RETVAL -eq 0 ] | ||
rm -f $LOCK_FILE | ||
rm -f ${PID} | ||
echo | ||
return $RETVAL | ||
do_restart() { | ||
do_stop | ||
do_start | ||
} | ||
|
||
# See how we were called. | ||
case "$1" in | ||
start) | ||
start | ||
;; | ||
stop) | ||
stop | ||
;; | ||
restart) | ||
stop | ||
start | ||
;; | ||
condrestart) | ||
if [ -f $LOCK_FILE ]; then | ||
stop | ||
start | ||
RETVAL=$? | ||
fi | ||
;; | ||
status) | ||
status $DAEMON | ||
RETVAL=$? | ||
;; | ||
*) | ||
echo $"Usage: $0 {start|stop|restart|condrestart|status}" | ||
RETVAL=1 | ||
start|stop|restart|status) | ||
do_$1 | ||
;; | ||
*) | ||
echo "Usage: shadowsocks {start|stop|restart|status}" | ||
RET_VAL=1 | ||
;; | ||
esac | ||
|
||
exit $RETVAL | ||
exit $RET_VAL |