aboutsummaryrefslogtreecommitdiffstats
path: root/unix/frpc-daemon.sh
blob: db6feda288f7dbb6aaca734bdb4f0560cf606265 (plain)
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
#!/bin/sh
#
# Start the `FRP client' as a daemon, and start/check with a cron job.
# https://github.com/fatedier/frp
#
# See start-stop-daemon(8)
#
# Example configuration file:
# ---------------------------------------------------------------------
# [common]
# server_addr = xxx.xxx.xxx.xxx
# server_port = xxxx
#
# [ssh]
# type = tcp
# local_ip = 127.0.0.1
# local_port = 22
# # port on the remote server that will be proxied to here
# # e.g., ssh -p <remote_port> <user>@<server_addr>
# remote_port = 2334
# ---------------------------------------------------------------------
#
# Example cron job (check every 10th minutes):
# */10  *  *  *  *  sh $HOME/bin/frpc-daemon.sh start >/dev/null 2>&1
#
#
# Aaron LI
# 2018-01-23
#

NAME=frpc
PROGRAM=$HOME/local/frp/frpc
CONFIG=$HOME/configs/${NAME}.ini
PIDFILE=$HOME/.cache/${NAME}.pid
LOGFILE=$HOME/.cache/${NAME}.log

PATH=${PATH}:/usr/local/sbin:/usr/sbin:/sbin

case "$1" in
    start)
        cmd=start
        cmd_args="--start --make-pidfile --background"
        ;;
    stop)
        cmd=stop
        cmd_args="--stop --remove-pidfile --retry 5"
        ;;
    status)
        cmd=status
        cmd_args="--status"
        ;;
    *)
        echo "usage: ${0##*/} {start|stop|status}"
        exit 1
        ;;
esac

start-stop-daemon ${cmd_args} \
    --oknodo --verbose \
    --name ${NAME} \
    --pidfile ${PIDFILE} \
    --startas ${PROGRAM} \
    -- -c ${CONFIG} -L ${LOGFILE}
ret=$?
if [ "${cmd}" = "start" ]; then
    if [ ${ret} -eq 0 ]; then
        echo "Successfully started ${NAME} as PID $(cat ${PIDFILE})"
    else
        echo "Failed to start ${NAME}: ${PROGRAM}"
        echo "Exit code: ${ret}"
    fi
elif [ "${cmd}" = "stop" ]; then
    if [ ${ret} -eq 0 ]; then
        echo "Successfully stopped ${NAME}: ${PROGRAM}"
    else
        echo "Failed to stop ${NAME}: ${PROGRAM}"
        echo "Exit code: ${ret}"
    fi
elif [ "${cmd}" = "status" ]; then
    if [ ${ret} -eq 0 ]; then
        echo "${NAME} is running as PID $(cat ${PIDFILE})"
    elif [ ${ret} -eq 1 ]; then
        echo "${NAME} is not running but the PID file (${PIDFILE}) exists"
    elif [ ${ret} -eq 3 ]; then
        echo "${NAME} is not running"
    else
        echo "${NAME} in unknown status"
        echo "Exit code: ${ret}"
    fi
else
    echo "ERROR: unknown command: ${cmd}"
fi