-
Notifications
You must be signed in to change notification settings - Fork 358
/
caddy_debian.sh
123 lines (107 loc) · 2.26 KB
/
caddy_debian.sh
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides: caddy
# Required-Start: $network $syslog
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: A Stable & Secure Tunnel Based On KCP with N:M Multiplexing
# Description: Start or stop the caddy server
### END INIT INFO
if [ -f /usr/local/caddy/caddy ]; then
DAEMON=/usr/local/caddy/caddy
fi
NAME=caddy
CONF=/usr/local/caddy/Caddyfile
PID_DIR=/var/run
PID_FILE=$PID_DIR/$NAME.pid
RET_VAL=0
[ -x $DAEMON ] || exit 0
check_pid(){
get_pid=`ps -ef |grep -v grep |grep -v "init.d" |grep -v "service" |grep $NAME |awk '{print $2}'`
}
check_pid
if [ -z $get_pid ]; then
if [ -e $PID_FILE ]; then
rm -f $PID_FILE
fi
fi
if [ ! -d $PID_DIR ]; then
mkdir -p $PID_DIR
if [ $? -ne 0 ]; then
echo "Creating PID directory $PID_DIR failed"
exit 1
fi
fi
if [ ! -f $CONF ]; then
echo "$NAME config file $CONF not found"
exit 1
fi
check_running() {
if [ -e $PID_FILE ]; then
if [ -r $PID_FILE ]; then
read PID < $PID_FILE
if [ -d "/proc/$PID" ]; then
return 0
else
rm -f $PID_FILE
return 1
fi
fi
else
return 2
fi
}
do_status() {
check_running
case $? in
0)
echo "$NAME (pid $PID) is running."
;;
1|2)
echo "$NAME is stopped"
RET_VAL=1
;;
esac
}
do_start() {
if check_running; then
echo "$NAME (pid $PID) is already running."
return 0
fi
ulimit -n 51200
nohup "$DAEMON" --conf="$CONF" -agree > /dev/null 2>&1 &
check_pid
echo $get_pid > $PID_FILE
if check_running; then
echo "Starting $NAME success"
else
echo "Starting $NAME failed"
RET_VAL=1
fi
}
do_stop() {
if check_running; then
kill -9 $PID
rm -f $PID_FILE
echo "Stopping $NAME success"
else
echo "$NAME is stopped"
RET_VAL=1
fi
}
do_restart() {
do_stop
sleep 0.5
do_start
}
case "$1" in
start|stop|restart|status)
do_$1
;;
*)
echo "Usage: $0 { start | stop | restart | status }"
RET_VAL=1
;;
esac
exit $RET_VAL