shadowsocks / shadowsocks-nodejs

https://github.com/shadowsocks/shadowsocks
Other
1.25k stars 724 forks source link

Auto reload on config.json change #21

Closed dallascao closed 11 years ago

dallascao commented 11 years ago

It would be great if the running shadowsocks is able to detect changes to config.json and reload automatically.

clowwindy commented 11 years ago

Good idea, but I think this solution is better, as it support every command:

https://github.com/remy/nodemon

dallascao commented 11 years ago

instead of using nodemon or node supervisor, use a bash script. It's much more reliable. I found sometimes both nodemon and server.js are accidentally dead.

Save the following to start.sh in the same folder with server.js and then add a crontab /3 * * * \ bash /some dir/start.sh

The system checks every 3 minutes on the config.json change. If there is some change from last check or the server.js stops running, it will be restarted.

#!/bin/bash
#get the script dir
SOURCE="${BASH_SOURCE[0]}"
DIR="$( dirname "$SOURCE" )"
while [ -h "$SOURCE" ]
do 
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
  DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd )"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
cd $DIR
tmp_file="$DIR"/tmp.txt
if [ $(ps ax | grep 'server.js' | wc -l) -gt 1 ]; then
    old_time=`cat "$tmp_file"`
    new_time=`ls -l "$DIR"/config.json`
    if [ "$old_time" == "$new_time" ]; then
        echo running and json not updated
        exit 0
    fi
fi
ls -l "$DIR"/config.json > "$tmp_file"
killall node
/usr/local/bin/node "$DIR"/server.js > /dev/null 2>&1 &
echo "restart done!"

The horrible side effect is that this script will kill all other node processes too if there are ("killall node").

clowwindy commented 11 years ago

Maybe you could try daemon