-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsingbox
55 lines (46 loc) · 1.31 KB
/
singbox
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
#!/bin/sh
# PROVIDE: singbox
# REQUIRE: NETWORKING
# KEYWORD: shutdown
. /etc/rc.subr
name="singbox"
rcvar="singbox_enable"
pidfile="/var/run/${name}.pid"
logfile="/var/log/sing-box.log"
command="/usr/local/bin/sing-box"
config="/usr/local/etc/sing-box/config.json"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
restart_cmd="${name}_restart"
status_cmd="${name}_status"
singbox_start() {
if [ -f "$pidfile" ] && kill -0 $(cat "$pidfile") 2>/dev/null; then
echo "${name} is already running." | tee -a "$logfile"
return 1
fi
echo "Starting sing-box..."
$command run -c "$config" >> "$logfile" 2>&1 &
echo "sing-box started! (PID: $!)"
echo $! > "$pidfile"
echo "$(date '+%Y-%m-%d %H:%M:%S') - ${name} started successfully." | tee -a "$logfile"
}
singbox_stop() {
echo "Stopping sing-box..."
pkill -f "$command"
echo "$(date '+%Y-%m-%d %H:%M:%S') - ${name} has stopped." | tee -a "$logfile"
}
singbox_restart() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - Restarting ${name}..." | tee -a "$logfile"
singbox_stop
sleep 1
singbox_start
}
singbox_status() {
if pgrep -f "$command" >/dev/null 2>&1; then
echo "sing-box is running!(PID: $(pgrep -f "$command"))"
else
echo "sing-box not running!"
fi
}
load_rc_config $name
run_rc_command "$1"