1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #!/bin/bash
BIN=/usr/bin/client_linux_amd64 USER=nobody GROUP=nobody PID_DIR=/var/run PID_FILE=$PID_DIR/kcptun.pid LOG_FILE=/var/log/kcptun CONF_FILE=/etc/kcptun.json
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 "kcptun running with PID $PID" ;; 1) echo "kcptun not running, remove PID file $PID_FILE" ;; 2) echo "Could not find PID file $PID_FILE, kcptun 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 "kcptun already running with PID $PID" return 0 fi echo "starting kcptun" sudo -u $USER -H -- $BIN -c $CONF_FILE >$LOG_FILE 2>&1 & PID=$! echo $PID > $PID_FILE sleep 0.3 if ! check_running; then echo "start failed" return 1 fi echo "kcptun running with PID $PID" return 0 }
do_stop() { if check_running; then echo "stopping kcptun 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|stop|restart|status) do_$1 ;; *) echo "Usage: kcptun {start|stop|restart|status}" RET_VAL=1 ;; esac
exit $RET_VAL
|