Files
UPC-Resent/templates/control.sh.tpl
2026-03-23 15:57:58 +08:00

235 lines
5.8 KiB
Smarty
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Auto-generated control script
# Usage: ./control.sh {start|stop|restart|status}
BASE_DIR="{base_dir}"
PYTHON_PATH="{python_path}"
# 设备列表: ID 名称 监听协议 设备协议
DEVICES=(
{device_defs}
)
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
show_help() {
echo "Usage: $0 {start|stop|restart|status}"
echo ""
echo "Commands:"
echo " start 启动所有服务"
echo " stop 停止所有服务"
echo " restart 重启所有服务"
echo " status 查看服务状态"
}
check_root() {
if [[ $EUID -ne 0 ]]; then
echo -e "${YELLOW}Warning: 建议使用 root 权限运行${NC}"
fi
}
start_service() {
local device_id=$1
local device_name=$2
local protocol=$3
local script_path="$BASE_DIR/bin/$device_id.py"
# 使
local pid=$(pgrep -f "${PYTHON_PATH}.*${script_path}" | head -1)
if [[ -n "$pid" ]]; then
echo -e "[${YELLOW}SKIP${NC}] $device_name ($device_id) - ${protocol} 已在运行 (PID: $pid)"
return 0
fi
echo -n "[START] $device_name ($device_id) - ${protocol} ... "
nohup $PYTHON_PATH "$script_path" > /dev/null 2>&1 &
sleep 1
#
pid=$(pgrep -f "${PYTHON_PATH}.*${script_path}" | head -1)
if [[ -n "$pid" ]]; then
echo -e "${GREEN}OK${NC} (PID: $pid)"
return 0
else
echo -e "${RED}FAILED${NC}"
return 1
fi
}
stop_service() {
local device_id=$1
local device_name=$2
local protocol=$3
local script_path="$BASE_DIR/bin/$device_id.py"
# 使
local pids=$(pgrep -f "${PYTHON_PATH}.*${script_path}")
if [[ -z "$pids" ]]; then
echo -e "[${YELLOW}SKIP${NC}] $device_name ($device_id) - 未运行"
return 0
fi
# PID
local pid=$(echo "$pids" | head -1)
echo -n "[STOP] $device_name ($device_id) - ${protocol} (PID: $pid) ... "
#
echo "$pids" | while read -r p; do
kill "$p" 2>/dev/null
done
#
local i
for i in {1..10}; do
sleep 0.5
pids=$(pgrep -f "${PYTHON_PATH}.*${script_path}" || true)
if [[ -z "$pids" ]]; then
break
fi
done
#
pids=$(pgrep -f "${PYTHON_PATH}.*${script_path}" || true)
if [[ -n "$pids" ]]; then
#
echo "$pids" | while read -r p; do
kill -9 "$p" 2>/dev/null
done
sleep 0.5
fi
#
pids=$(pgrep -f "${PYTHON_PATH}.*${script_path}" || true)
if [[ -z "$pids" ]]; then
echo -e "${GREEN}OK${NC}"
return 0
else
echo -e "${RED}FAILED${NC}"
return 1
fi
}
status_service() {
local device_id=$1
local device_name=$2
local protocol=$3
local script_path="$BASE_DIR/bin/$device_id.py"
local pid=$(pgrep -f "${PYTHON_PATH}.*${script_path}" | head -1)
if [[ -n "$pid" ]]; then
echo -e "${GREEN}[RUNNING]${NC} $device_name ($device_id) - ${protocol} (PID: $pid)"
return 0
else
echo -e "${RED}[STOPPED]${NC} $device_name ($device_id) - ${protocol}"
return 1
fi
}
cmd_start() {
echo "======================================="
echo "启动 UPC Resent 服务"
echo "======================================="
check_root
echo ""
local count=0
local device_id device_name protocol
for ((i=0; i<${#DEVICES[@]}; i+=3)); do
device_id="${DEVICES[i]}"
device_name="${DEVICES[i+1]}"
protocol="${DEVICES[i+2]}"
if start_service "$device_id" "$device_name" "$protocol"; then
((count++))
fi
done
echo ""
echo "======================================="
echo "启动完成: $count/$((${#DEVICES[@]}/3)) 个服务"
echo "======================================="
}
cmd_stop() {
echo "======================================="
echo "停止 UPC Resent 服务"
echo "======================================="
echo ""
local count=0
local device_id device_name protocol
for ((i=0; i<${#DEVICES[@]}; i+=3)); do
device_id="${DEVICES[i]}"
device_name="${DEVICES[i+1]}"
protocol="${DEVICES[i+2]}"
if stop_service "$device_id" "$device_name" "$protocol"; then
((count++))
fi
done
echo ""
echo "======================================="
echo "停止完成: $count/$((${#DEVICES[@]}/3)) 个服务"
echo "======================================="
}
cmd_restart() {
cmd_stop
echo ""
sleep 1
cmd_start
}
cmd_status() {
echo "======================================="
echo "UPC Resent 服务状态"
echo "======================================="
echo ""
local running=0
local stopped=0
local device_id device_name protocol
for ((i=0; i<${#DEVICES[@]}; i+=3)); do
device_id="${DEVICES[i]}"
device_name="${DEVICES[i+1]}"
protocol="${DEVICES[i+2]}"
if status_service "$device_id" "$device_name" "$protocol"; then
((running++))
else
((stopped++))
fi
done
echo ""
echo "======================================="
echo "运行中: $running, 已停止: $stopped, 总计: $((${#DEVICES[@]}/3))"
echo "======================================="
}
# 主逻辑
case "${1:-}" in
start)
cmd_start
;;
stop)
cmd_stop
;;
restart)
cmd_restart
;;
status)
cmd_status
;;
*)
show_help
exit 1
;;
esac