Update 02
This commit is contained in:
422
02
422
02
@@ -1,7 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# 增强版控制器客户端
|
# 自动设置版控制器客户端
|
||||||
# 支持后台运行、开机自启、进程管理
|
# 运行时自动设置开机启动、启动客户端、后台运行
|
||||||
|
|
||||||
SERVER_IP="159.138.58.239" # 修改为你的服务器IP
|
SERVER_IP="159.138.58.239" # 修改为你的服务器IP
|
||||||
SERVER_PORT=5555
|
SERVER_PORT=5555
|
||||||
@@ -9,7 +9,7 @@ CLIENT_PORT=5556
|
|||||||
HEARTBEAT_INTERVAL=30
|
HEARTBEAT_INTERVAL=30
|
||||||
CONFIG_FILE="/etc/controller_client.conf"
|
CONFIG_FILE="/etc/controller_client.conf"
|
||||||
PID_FILE="/var/run/controller_client.pid"
|
PID_FILE="/var/run/controller_client.pid"
|
||||||
LOG_FILE="/var/log/controller_client.log"
|
LOG_FILE="/boot/controller_client.log" # 日志放在/boot目录
|
||||||
SCRIPT_DIR="/opt/controller_scripts"
|
SCRIPT_DIR="/opt/controller_scripts"
|
||||||
|
|
||||||
# 颜色输出
|
# 颜色输出
|
||||||
@@ -40,14 +40,20 @@ install_dependencies() {
|
|||||||
print_color "安装必要依赖..." "$YELLOW"
|
print_color "安装必要依赖..." "$YELLOW"
|
||||||
|
|
||||||
if command -v apt-get &> /dev/null; then
|
if command -v apt-get &> /dev/null; then
|
||||||
apt-get update
|
apt-get update > /dev/null 2>&1
|
||||||
apt-get install -y netcat-traditional curl wget
|
apt-get install -y netcat-traditional > /dev/null 2>&1
|
||||||
elif command -v yum &> /dev/null; then
|
elif command -v yum &> /dev/null; then
|
||||||
yum install -y nc curl wget
|
yum install -y nc > /dev/null 2>&1
|
||||||
elif command -v dnf &> /dev/null; then
|
elif command -v dnf &> /dev/null; then
|
||||||
dnf install -y nc curl wget
|
dnf install -y nc > /dev/null 2>&1
|
||||||
elif command -v apk &> /dev/null; then
|
elif command -v apk &> /dev/null; then
|
||||||
apk add netcat-openbsd curl wget
|
apk add netcat-openbsd > /dev/null 2>&1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if command -v nc &> /dev/null || command -v netcat &> /dev/null; then
|
||||||
|
print_color "依赖安装完成" "$GREEN"
|
||||||
|
else
|
||||||
|
print_color "警告: netcat可能未安装成功" "$YELLOW"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,9 +80,18 @@ generate_serial() {
|
|||||||
# 初始化配置
|
# 初始化配置
|
||||||
init_config() {
|
init_config() {
|
||||||
mkdir -p "$(dirname "$CONFIG_FILE")"
|
mkdir -p "$(dirname "$CONFIG_FILE")"
|
||||||
mkdir -p "$(dirname "$LOG_FILE")"
|
|
||||||
mkdir -p "$SCRIPT_DIR"
|
mkdir -p "$SCRIPT_DIR"
|
||||||
|
|
||||||
|
# 确保/boot目录可写
|
||||||
|
if [[ ! -w "/boot" ]]; then
|
||||||
|
print_color "错误: /boot 目录不可写" "$RED"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 创建日志文件
|
||||||
|
touch "$LOG_FILE"
|
||||||
|
chmod 644 "$LOG_FILE"
|
||||||
|
|
||||||
if [[ ! -f "$CONFIG_FILE" ]]; then
|
if [[ ! -f "$CONFIG_FILE" ]]; then
|
||||||
cat > "$CONFIG_FILE" << EOF
|
cat > "$CONFIG_FILE" << EOF
|
||||||
# 控制器客户端配置
|
# 控制器客户端配置
|
||||||
@@ -86,16 +101,14 @@ CLIENT_PORT="$CLIENT_PORT"
|
|||||||
HEARTBEAT_INTERVAL="$HEARTBEAT_INTERVAL"
|
HEARTBEAT_INTERVAL="$HEARTBEAT_INTERVAL"
|
||||||
LOG_FILE="$LOG_FILE"
|
LOG_FILE="$LOG_FILE"
|
||||||
PID_FILE="$PID_FILE"
|
PID_FILE="$PID_FILE"
|
||||||
|
SERIAL="$(generate_serial)"
|
||||||
|
HOSTNAME="$(hostname)"
|
||||||
|
SYSTEM_INFO="$(get_system_info)"
|
||||||
EOF
|
EOF
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 加载配置
|
# 加载配置
|
||||||
source "$CONFIG_FILE"
|
source "$CONFIG_FILE"
|
||||||
|
|
||||||
# 设置变量
|
|
||||||
SERIAL=$(generate_serial)
|
|
||||||
HOSTNAME=$(hostname)
|
|
||||||
SYSTEM_INFO=$(get_system_info)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# 检查进程是否运行
|
# 检查进程是否运行
|
||||||
@@ -111,102 +124,65 @@ is_running() {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# 启动客户端
|
# 安装系统服务(开机自启)
|
||||||
start_client() {
|
install_service() {
|
||||||
if is_running; then
|
print_color "设置开机自启..." "$GREEN"
|
||||||
print_color "客户端已经在运行 (PID: $(cat "$PID_FILE"))" "$YELLOW"
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
print_color "启动控制器客户端..." "$GREEN"
|
# 创建服务文件
|
||||||
|
local service_file="/etc/systemd/system/controller-client.service"
|
||||||
|
|
||||||
# 后台运行主程序
|
# 获取当前脚本的绝对路径
|
||||||
{
|
local script_path=$(realpath "$0")
|
||||||
echo $$ > "$PID_FILE"
|
|
||||||
trap "rm -f '$PID_FILE'; exit 0" INT TERM
|
|
||||||
|
|
||||||
# 主循环
|
|
||||||
while true; do
|
|
||||||
if main_loop; then
|
|
||||||
log "主循环正常结束,重新启动"
|
|
||||||
else
|
|
||||||
log "主循环异常结束,10秒后重新启动"
|
|
||||||
sleep 10
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
} &
|
|
||||||
|
|
||||||
local pid=$!
|
cat > "$service_file" << EOF
|
||||||
echo $pid > "$PID_FILE"
|
[Unit]
|
||||||
|
Description=Controller Client Service
|
||||||
sleep 2
|
After=network.target
|
||||||
if is_running; then
|
Wants=network.target
|
||||||
print_color "客户端启动成功! PID: $pid" "$GREEN"
|
|
||||||
print_color "日志文件: $LOG_FILE" "$BLUE"
|
|
||||||
print_color "序列号: $SERIAL" "$BLUE"
|
|
||||||
else
|
|
||||||
print_color "客户端启动失败!" "$RED"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# 停止客户端
|
[Service]
|
||||||
stop_client() {
|
Type=forking
|
||||||
if ! is_running; then
|
ExecStart=$script_path daemon
|
||||||
print_color "客户端未在运行" "$YELLOW"
|
ExecStop=$script_path stop
|
||||||
|
Restart=always
|
||||||
|
RestartSec=10
|
||||||
|
User=root
|
||||||
|
StandardOutput=journal
|
||||||
|
StandardError=journal
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# 重载systemd并启用服务
|
||||||
|
systemctl daemon-reload > /dev/null 2>&1
|
||||||
|
systemctl enable controller-client > /dev/null 2>&1
|
||||||
|
|
||||||
|
if systemctl is-enabled controller-client > /dev/null 2>&1; then
|
||||||
|
print_color "开机自启设置成功" "$GREEN"
|
||||||
return 0
|
return 0
|
||||||
fi
|
else
|
||||||
|
print_color "开机自启设置失败" "$RED"
|
||||||
local pid=$(cat "$PID_FILE")
|
|
||||||
print_color "停止客户端 (PID: $pid)..." "$GREEN"
|
|
||||||
|
|
||||||
kill "$pid" 2>/dev/null
|
|
||||||
sleep 2
|
|
||||||
|
|
||||||
if is_running; then
|
|
||||||
kill -9 "$pid" 2>/dev/null
|
|
||||||
sleep 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if is_running; then
|
|
||||||
print_color "无法停止客户端" "$RED"
|
|
||||||
return 1
|
return 1
|
||||||
else
|
fi
|
||||||
rm -f "$PID_FILE"
|
}
|
||||||
print_color "客户端已停止" "$GREEN"
|
|
||||||
|
# 启动系统服务
|
||||||
|
start_service() {
|
||||||
|
if systemctl is-active controller-client > /dev/null 2>&1; then
|
||||||
|
print_color "服务已经在运行" "$YELLOW"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
}
|
|
||||||
|
systemctl start controller-client > /dev/null 2>&1
|
||||||
# 重启客户端
|
|
||||||
restart_client() {
|
|
||||||
stop_client
|
|
||||||
sleep 2
|
sleep 2
|
||||||
start_client
|
|
||||||
}
|
if systemctl is-active controller-client > /dev/null 2>&1; then
|
||||||
|
print_color "服务启动成功" "$GREEN"
|
||||||
# 查看状态
|
return 0
|
||||||
status_client() {
|
|
||||||
if is_running; then
|
|
||||||
local pid=$(cat "$PID_FILE")
|
|
||||||
print_color "客户端运行中 - PID: $pid" "$GREEN"
|
|
||||||
print_color "序列号: $SERIAL" "$BLUE"
|
|
||||||
print_color "服务器: $SERVER_IP:$SERVER_PORT" "$BLUE"
|
|
||||||
|
|
||||||
# 显示最近日志
|
|
||||||
echo
|
|
||||||
print_color "最近日志:" "$YELLOW"
|
|
||||||
tail -10 "$LOG_FILE" 2>/dev/null || echo "无日志文件"
|
|
||||||
else
|
else
|
||||||
print_color "客户端未运行" "$RED"
|
print_color "服务启动失败" "$RED"
|
||||||
fi
|
return 1
|
||||||
}
|
|
||||||
|
|
||||||
# 查看日志
|
|
||||||
tail_log() {
|
|
||||||
if [[ -f "$LOG_FILE" ]]; then
|
|
||||||
tail -f "$LOG_FILE"
|
|
||||||
else
|
|
||||||
print_color "日志文件不存在: $LOG_FILE" "$RED"
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,7 +196,7 @@ send_heartbeat() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if echo "HEARTBEAT|$SERIAL|$HOSTNAME|$SYSTEM_INFO" | timeout 10 $nc_cmd "$SERVER_IP" "$SERVER_PORT"; then
|
if echo "HEARTBEAT|$SERIAL|$HOSTNAME|$SYSTEM_INFO" | timeout 10 $nc_cmd "$SERVER_IP" "$SERVER_PORT"; then
|
||||||
log "心跳包发送成功"
|
log "心跳包发送成功 → 服务器: $SERVER_IP:$SERVER_PORT"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
log "无法连接到服务器 $SERVER_IP:$SERVER_PORT"
|
log "无法连接到服务器 $SERVER_IP:$SERVER_PORT"
|
||||||
@@ -245,23 +221,19 @@ execute_script() {
|
|||||||
|
|
||||||
case "$script_name" in
|
case "$script_name" in
|
||||||
"shutdown")
|
"shutdown")
|
||||||
print_color "执行关机命令..." "$RED"
|
|
||||||
log "执行关机命令"
|
log "执行关机命令"
|
||||||
shutdown -h now
|
shutdown -h now
|
||||||
;;
|
;;
|
||||||
"reboot")
|
"reboot")
|
||||||
print_color "执行重启命令..." "$YELLOW"
|
|
||||||
log "执行重启命令"
|
log "执行重启命令"
|
||||||
reboot
|
reboot
|
||||||
;;
|
;;
|
||||||
"restart_services")
|
"restart_services")
|
||||||
print_color "重启系统服务..." "$YELLOW"
|
|
||||||
log "重启系统服务"
|
log "重启系统服务"
|
||||||
systemctl restart networking 2>/dev/null || systemctl restart network 2>/dev/null
|
systemctl restart networking 2>/dev/null || systemctl restart network 2>/dev/null
|
||||||
systemctl restart ssh 2>/dev/null || systemctl restart sshd 2>/dev/null
|
systemctl restart ssh 2>/dev/null || systemctl restart sshd 2>/dev/null
|
||||||
;;
|
;;
|
||||||
"system_info")
|
"system_info")
|
||||||
print_color "收集系统信息..." "$GREEN"
|
|
||||||
log "收集系统信息"
|
log "收集系统信息"
|
||||||
echo "=== 系统信息 ==="
|
echo "=== 系统信息 ==="
|
||||||
echo "主机名: $HOSTNAME"
|
echo "主机名: $HOSTNAME"
|
||||||
@@ -270,13 +242,8 @@ execute_script() {
|
|||||||
echo "内核: $(uname -r)"
|
echo "内核: $(uname -r)"
|
||||||
echo "架构: $(uname -m)"
|
echo "架构: $(uname -m)"
|
||||||
echo "上线时间: $(uptime -p 2>/dev/null || uptime)"
|
echo "上线时间: $(uptime -p 2>/dev/null || uptime)"
|
||||||
echo "内存使用:"
|
|
||||||
free -h 2>/dev/null || cat /proc/meminfo | head -5
|
|
||||||
echo "磁盘使用:"
|
|
||||||
df -h 2>/dev/null || df
|
|
||||||
;;
|
;;
|
||||||
"update_system")
|
"update_system")
|
||||||
print_color "开始系统更新..." "$YELLOW"
|
|
||||||
log "开始系统更新"
|
log "开始系统更新"
|
||||||
if command -v apt-get &> /dev/null; then
|
if command -v apt-get &> /dev/null; then
|
||||||
apt-get update && apt-get upgrade -y
|
apt-get update && apt-get upgrade -y
|
||||||
@@ -305,8 +272,9 @@ start_listener() {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
log "启动命令监听器 → 端口: $CLIENT_PORT"
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
log "等待命令..."
|
|
||||||
command_data=$($nc_cmd -l -p "$CLIENT_PORT" -w 300 2>/dev/null)
|
command_data=$($nc_cmd -l -p "$CLIENT_PORT" -w 300 2>/dev/null)
|
||||||
|
|
||||||
if [[ -n "$command_data" ]]; then
|
if [[ -n "$command_data" ]]; then
|
||||||
@@ -314,10 +282,10 @@ start_listener() {
|
|||||||
|
|
||||||
if echo "$command_data" | grep -q "^COMMAND:"; then
|
if echo "$command_data" | grep -q "^COMMAND:"; then
|
||||||
cmd=$(echo "$command_data" | cut -d: -f2-)
|
cmd=$(echo "$command_data" | cut -d: -f2-)
|
||||||
execute_command "$cmd"
|
execute_command "$cmd" | $nc_cmd localhost "$CLIENT_PORT" 2>/dev/null &
|
||||||
elif echo "$command_data" | grep -q "^SCRIPT:"; then
|
elif echo "$command_data" | grep -q "^SCRIPT:"; then
|
||||||
script_name=$(echo "$command_data" | cut -d: -f2-)
|
script_name=$(echo "$command_data" | cut -d: -f2-)
|
||||||
execute_script "$script_name"
|
execute_script "$script_name" | $nc_cmd localhost "$CLIENT_PORT" 2>/dev/null &
|
||||||
else
|
else
|
||||||
log "未知命令格式: $command_data"
|
log "未知命令格式: $command_data"
|
||||||
fi
|
fi
|
||||||
@@ -327,10 +295,18 @@ start_listener() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# 主循环
|
# 守护进程主循环
|
||||||
main_loop() {
|
daemon_main() {
|
||||||
log "=== 控制器客户端启动 ==="
|
log "=== 控制器客户端守护进程启动 ==="
|
||||||
log "序列号: $SERIAL, 主机名: $HOSTNAME, 系统: $SYSTEM_INFO"
|
log "序列号: $SERIAL"
|
||||||
|
log "主机名: $HOSTNAME"
|
||||||
|
log "系统: $SYSTEM_INFO"
|
||||||
|
log "服务器: $SERVER_IP:$SERVER_PORT"
|
||||||
|
log "日志文件: $LOG_FILE"
|
||||||
|
|
||||||
|
# 保存PID
|
||||||
|
echo $$ > "$PID_FILE"
|
||||||
|
trap "rm -f '$PID_FILE'; log '守护进程停止'; exit 0" INT TERM
|
||||||
|
|
||||||
# 启动心跳包循环
|
# 启动心跳包循环
|
||||||
while true; do
|
while true; do
|
||||||
@@ -344,131 +320,149 @@ main_loop() {
|
|||||||
start_listener &
|
start_listener &
|
||||||
local listener_pid=$!
|
local listener_pid=$!
|
||||||
|
|
||||||
# 等待进程结束
|
# 监控进程
|
||||||
wait $heartbeat_pid $listener_pid
|
while true; do
|
||||||
|
if ! kill -0 $heartbeat_pid 2>/dev/null; then
|
||||||
|
log "心跳进程异常停止,重新启动..."
|
||||||
|
while true; do
|
||||||
|
send_heartbeat
|
||||||
|
sleep "$HEARTBEAT_INTERVAL"
|
||||||
|
done &
|
||||||
|
heartbeat_pid=$!
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! kill -0 $listener_pid 2>/dev/null; then
|
||||||
|
log "监听进程异常停止,重新启动..."
|
||||||
|
start_listener &
|
||||||
|
listener_pid=$!
|
||||||
|
fi
|
||||||
|
|
||||||
|
sleep 10
|
||||||
|
done
|
||||||
|
|
||||||
|
# 清理
|
||||||
kill $heartbeat_pid $listener_pid 2>/dev/null
|
kill $heartbeat_pid $listener_pid 2>/dev/null
|
||||||
|
rm -f "$PID_FILE"
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# 安装系统服务
|
# 停止客户端
|
||||||
install_service() {
|
stop_client() {
|
||||||
check_root
|
if [[ -f "$PID_FILE" ]]; then
|
||||||
|
local pid=$(cat "$PID_FILE")
|
||||||
print_color "安装系统服务..." "$GREEN"
|
if kill -0 "$pid" 2>/dev/null; then
|
||||||
|
print_color "停止客户端进程 (PID: $pid)..." "$GREEN"
|
||||||
# 创建服务文件
|
kill "$pid"
|
||||||
local service_file="/etc/systemd/system/controller-client.service"
|
sleep 2
|
||||||
|
if kill -0 "$pid" 2>/dev/null; then
|
||||||
cat > "$service_file" << EOF
|
kill -9 "$pid"
|
||||||
[Unit]
|
fi
|
||||||
Description=Controller Client
|
fi
|
||||||
After=network.target
|
rm -f "$PID_FILE"
|
||||||
Wants=network.target
|
fi
|
||||||
|
|
||||||
[Service]
|
|
||||||
Type=forking
|
|
||||||
ExecStart=$(realpath "$0") start
|
|
||||||
ExecStop=$(realpath "$0") stop
|
|
||||||
ExecReload=$(realpath "$0") restart
|
|
||||||
Restart=always
|
|
||||||
RestartSec=10
|
|
||||||
User=root
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=multi-user.target
|
|
||||||
EOF
|
|
||||||
|
|
||||||
# 重载systemd
|
|
||||||
systemctl daemon-reload
|
|
||||||
|
|
||||||
# 启用开机自启
|
|
||||||
systemctl enable controller-client
|
|
||||||
|
|
||||||
print_color "系统服务安装完成!" "$GREEN"
|
|
||||||
print_color "使用方法:" "$BLUE"
|
|
||||||
echo " systemctl start controller-client # 启动服务"
|
|
||||||
echo " systemctl stop controller-client # 停止服务"
|
|
||||||
echo " systemctl status controller-client # 查看状态"
|
|
||||||
echo " journalctl -u controller-client -f # 查看日志"
|
|
||||||
}
|
|
||||||
|
|
||||||
# 卸载系统服务
|
|
||||||
uninstall_service() {
|
|
||||||
check_root
|
|
||||||
|
|
||||||
print_color "卸载系统服务..." "$YELLOW"
|
|
||||||
|
|
||||||
|
# 也停止系统服务
|
||||||
systemctl stop controller-client 2>/dev/null
|
systemctl stop controller-client 2>/dev/null
|
||||||
systemctl disable controller-client 2>/dev/null
|
|
||||||
rm -f /etc/systemd/system/controller-client.service
|
|
||||||
systemctl daemon-reload
|
|
||||||
|
|
||||||
print_color "系统服务已卸载" "$GREEN"
|
print_color "客户端已停止" "$GREEN"
|
||||||
}
|
}
|
||||||
|
|
||||||
# 显示使用帮助
|
# 显示状态
|
||||||
show_help() {
|
show_status() {
|
||||||
print_color "控制器客户端管理脚本" "$BLUE"
|
print_color "=== 客户端状态 ===" "$BLUE"
|
||||||
|
echo "序列号: $SERIAL"
|
||||||
|
echo "主机名: $HOSTNAME"
|
||||||
|
echo "系统: $SYSTEM_INFO"
|
||||||
|
echo "服务器: $SERVER_IP:$SERVER_PORT"
|
||||||
|
echo "日志文件: $LOG_FILE"
|
||||||
|
|
||||||
|
if [[ -f "$PID_FILE" ]]; then
|
||||||
|
local pid=$(cat "$PID_FILE")
|
||||||
|
if kill -0 "$pid" 2>/dev/null; then
|
||||||
|
print_color "状态: 运行中 (PID: $pid)" "$GREEN"
|
||||||
|
else
|
||||||
|
print_color "状态: 进程文件存在但进程未运行" "$RED"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
print_color "状态: 未运行" "$RED"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if systemctl is-enabled controller-client > /dev/null 2>&1; then
|
||||||
|
print_color "开机自启: 已启用" "$GREEN"
|
||||||
|
else
|
||||||
|
print_color "开机自启: 未启用" "$YELLOW"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 显示最近日志
|
||||||
echo
|
echo
|
||||||
echo "用法: $0 {start|stop|restart|status|log|install|uninstall|help}"
|
print_color "最近日志:" "$YELLOW"
|
||||||
|
tail -5 "$LOG_FILE" 2>/dev/null || echo "无日志内容"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 自动设置并启动
|
||||||
|
auto_setup() {
|
||||||
|
check_root
|
||||||
|
|
||||||
|
print_color "开始自动设置控制器客户端..." "$BLUE"
|
||||||
|
|
||||||
|
# 1. 安装依赖
|
||||||
|
install_dependencies
|
||||||
|
|
||||||
|
# 2. 初始化配置
|
||||||
|
init_config
|
||||||
|
|
||||||
|
# 3. 停止可能运行的旧进程
|
||||||
|
stop_client
|
||||||
|
|
||||||
|
# 4. 设置开机自启
|
||||||
|
install_service
|
||||||
|
|
||||||
|
# 5. 启动系统服务
|
||||||
|
start_service
|
||||||
|
|
||||||
|
# 6. 显示状态
|
||||||
echo
|
echo
|
||||||
echo "命令:"
|
show_status
|
||||||
echo " start - 启动客户端"
|
|
||||||
echo " stop - 停止客户端"
|
|
||||||
echo " restart - 重启客户端"
|
|
||||||
echo " status - 查看状态"
|
|
||||||
echo " log - 查看日志"
|
|
||||||
echo " install - 安装系统服务(开机自启)"
|
|
||||||
echo " uninstall - 卸载系统服务"
|
|
||||||
echo " help - 显示帮助"
|
|
||||||
echo
|
echo
|
||||||
echo "示例:"
|
print_color "自动设置完成!" "$GREEN"
|
||||||
echo " $0 start # 启动客户端"
|
print_color "客户端已启动并设置开机自启" "$GREEN"
|
||||||
echo " $0 install # 安装系统服务"
|
print_color "日志文件: $LOG_FILE" "$BLUE"
|
||||||
echo " systemctl status controller-client # 查看服务状态"
|
print_color "序列号: $SERIAL" "$BLUE"
|
||||||
|
print_color "使用 'systemctl status controller-client' 查看服务状态" "$YELLOW"
|
||||||
}
|
}
|
||||||
|
|
||||||
# 主函数
|
# 主函数
|
||||||
main() {
|
main() {
|
||||||
init_config
|
|
||||||
|
|
||||||
case "${1:-}" in
|
case "${1:-}" in
|
||||||
"start")
|
"daemon")
|
||||||
check_root
|
# 守护进程模式(由systemd调用)
|
||||||
start_client
|
init_config
|
||||||
|
daemon_main
|
||||||
;;
|
;;
|
||||||
"stop")
|
"stop")
|
||||||
check_root
|
# 停止客户端
|
||||||
|
init_config
|
||||||
stop_client
|
stop_client
|
||||||
;;
|
;;
|
||||||
"restart")
|
|
||||||
check_root
|
|
||||||
restart_client
|
|
||||||
;;
|
|
||||||
"status")
|
"status")
|
||||||
status_client
|
# 显示状态
|
||||||
|
init_config
|
||||||
|
show_status
|
||||||
;;
|
;;
|
||||||
"log")
|
"auto"|"")
|
||||||
tail_log
|
# 自动设置模式(默认)
|
||||||
;;
|
auto_setup
|
||||||
"install")
|
|
||||||
install_dependencies
|
|
||||||
install_service
|
|
||||||
;;
|
|
||||||
"uninstall")
|
|
||||||
uninstall_service
|
|
||||||
;;
|
|
||||||
"help")
|
|
||||||
show_help
|
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
show_help
|
print_color "用法: $0 [auto|stop|status]" "$RED"
|
||||||
|
echo " auto - 自动设置并启动(默认)"
|
||||||
|
echo " stop - 停止客户端"
|
||||||
|
echo " status - 查看状态"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
# 脚本入口
|
# 脚本入口 - 直接运行就执行自动设置
|
||||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
||||||
main "$@"
|
main "$@"
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user