#!/bin/bash # Browser VNC Manager DAEMON_SCRIPT="$(dirname $0)/start-browser-vnc.sh" BASE_VNC_PORT=5900 BASE_DATA_DIR="/tmp/chrome-profiles" # 用户数据目录 start() { local instance=${1:-1} local vnc_pwd=${2:-"123456"} local display_num=$((98 + instance)) local vnc_port=$((BASE_VNC_PORT + instance - 1)) local data_dir="${BASE_DATA_DIR}/instance-${instance}" if [ -f "/tmp/chrome-browser-${display_num}.pid" ] && kill -0 $(cat "/tmp/chrome-browser-${display_num}.pid") 2>/dev/null; then echo "Instance ${instance} already running" return 1 fi echo "Starting instance ${instance} (display :${display_num}, VNC port: ${vnc_port})..." echo "Data directory: ${data_dir}" # 确保数据目录存在 mkdir -p "$data_dir" nohup "$DAEMON_SCRIPT" "$vnc_pwd" "$vnc_port" "$display_num" "$data_dir" >> "/tmp/chrome-browser-${display_num}.log" 2>&1 & sleep 3 local public_ip=$(hostname -I | awk '{print $1}') echo "✅ Instance ${instance} started" echo " VNC: ${public_ip}:${vnc_port}" echo " Password: ${vnc_pwd}" echo " Data: ${data_dir} (will be deleted on stop)" } stop() { local instance=${1:-1} local display_num=$((98 + instance)) local pid_file="/tmp/chrome-browser-${display_num}.pid" local data_dir="${BASE_DATA_DIR}/instance-${instance}" local delete_data=${2:-"true"} # 默认删除数据 if [ -f "$pid_file" ]; then local pid=$(cat "$pid_file") echo "Stopping instance ${instance} (PID: $pid)..." # 先发送 TERM 信号,让浏览器优雅关闭 kill -TERM $pid 2>/dev/null sleep 3 # 如果还在运行,强制杀死 if kill -0 $pid 2>/dev/null; then echo "Force killing..." kill -9 $pid 2>/dev/null fi # 删除 PID 文件 rm -f "$pid_file" # 删除用户数据目录 if [ "$delete_data" = "true" ] && [ -d "$data_dir" ]; then echo "Deleting user data: ${data_dir}" rm -rf "$data_dir" fi echo "✅ Instance ${instance} stopped" else echo "Instance ${instance} not running, cleaning up data..." # 即使进程不在运行,也清理数据目录 if [ -d "$data_dir" ] && [ "$delete_data" = "true" ]; then rm -rf "$data_dir" echo "✅ Data cleaned: ${data_dir}" fi fi # 额外清理:杀掉可能残留的 Xvfb 和 x11vnc 进程 pkill -f "Xvfb :${display_num}" 2>/dev/null pkill -f "x11vnc.*${display_num}" 2>/dev/null # 清理临时文件 rm -f "/tmp/.X${display_num}-lock" 2>/dev/null rm -f "/tmp/.X11-unix/X${display_num}" 2>/dev/null } status() { echo "=== Browser Instances (VNC only) ===" for i in {1..10}; do local display_num=$((98 + i)) local pid_file="/tmp/chrome-browser-${display_num}.pid" local data_dir="${BASE_DATA_DIR}/instance-${i}" if [ -f "$pid_file" ] && kill -0 $(cat "$pid_file") 2>/dev/null; then local pid=$(cat "$pid_file") echo "Instance ${i}: ✅ Running (VNC: $((5900 + i - 1)), PID: ${pid}, Data: ${data_dir})" else echo "Instance ${i}: ❌ Stopped" fi done } stop_all() { echo "Stopping all instances..." for i in {1..10}; do stop $i "${1:-true}" done echo "✅ All instances stopped" } cleanup_data() { local instance=${1:-1} local data_dir="${BASE_DATA_DIR}/instance-${instance}" # 先停止实例 stop $instance true # 如果还有残留数据,强制删除 if [ -d "$data_dir" ]; then echo "Force cleaning data: ${data_dir}" rm -rf "$data_dir" fi echo "✅ Data for instance ${instance} cleaned" } cleanup_all_data() { echo "Cleaning all instance data..." for i in {1..10}; do local data_dir="${BASE_DATA_DIR}/instance-${i}" if [ -d "$data_dir" ]; then rm -rf "$data_dir" echo " Removed: ${data_dir}" fi done echo "✅ All data cleaned" } case "${1:-}" in start) start "${2:-1}" "${3:-123456}" ;; stop) stop "${2:-1}" "${3:-true}" ;; stop-keep-data) stop "${2:-1}" "false" # 停止但保留数据 ;; stop-all) stop_all "true" ;; stop-all-keep-data) stop_all "false" ;; status) status ;; cleanup) cleanup_data "${2:-1}" ;; cleanup-all) cleanup_all_data ;; *) echo "Usage: $0 {start|stop|stop-keep-data|stop-all|stop-all-keep-data|status|cleanup|cleanup-all} [instance_num] [password]" echo "" echo "Commands:" echo " start [num] [pwd] - Start instance (default: 1, 123456)" echo " stop [num] - Stop and DELETE user data" echo " stop-keep-data [num] - Stop but KEEP user data" echo " stop-all - Stop all and DELETE all user data" echo " stop-all-keep-data - Stop all but KEEP user data" echo " status - Show all instances status" echo " cleanup [num] - Force delete user data for instance" echo " cleanup-all - Force delete all user data" echo "" echo "Examples:" echo " $0 start 1 # Start instance 1 (VNC:5900, password:123456)" echo " $0 start 2 mypass # Start instance 2 (VNC:5901, password:mypass)" echo " $0 stop 1 # Stop instance 1 and DELETE its data" echo " $0 stop-keep-data 1 # Stop instance 1 but KEEP its data" echo " $0 status # Show all instances" echo " $0 cleanup-all # Delete all instance data" exit 1 ;; esac