diff --git a/start.sh b/start.sh index 8e7df30..65ca2fb 100644 --- a/start.sh +++ b/start.sh @@ -23,52 +23,44 @@ cd /home/unitree/robot_face_rec || { echo "Env before display detection: DISPLAY=$DISPLAY, XAUTHORITY=$XAUTHORITY" } >> "$LOG_FILE" -# --- DISPLAY 设置逻辑 --- -# 使用默认DISPLAY值:0,这是Linux系统中最常见的显示编号 -display_found=":0" -echo "Using default DISPLAY: :0" +# --- DISPLAY 和 XAUTHORITY 设置逻辑 --- -for pid in $(pgrep Xorg); do - if [ -r /proc/$pid/environ ]; then - disp=$(tr '\0' '\n' < /proc/$pid/environ | grep '^DISPLAY=' | cut -d'=' -f2-) - if [ -n "$disp" ]; then - display_found="$disp" - echo "Chosen DISPLAY from Xorg process: $disp" - break - fi - fi -done +# 设置DISPLAY环境变量 +# 使用Linux系统最常见的默认值:0 +export DISPLAY=":0" +echo "Using DISPLAY: $DISPLAY" -# 如果还没找到 DISPLAY,则尝试 fallback -if [ -z "$display_found" ]; then - # 常见默认::0 - display_found=":0" - echo "Chosen default DISPLAY: :0" -fi - -export DISPLAY="$display_found" - -# 从Xorg命令行参数中获取正确的XAUTHORITY路径 +# 设置XAUTHORITY环境变量 +# 先尝试从Xorg进程命令行获取 xauth_path="" for pid in $(pgrep Xorg); do if [ -r /proc/$pid/cmdline ]; then cmdline=$(tr '\0' ' ' < /proc/$pid/cmdline) - xauth_path=$(echo "$cmdline" | grep -o "-auth [^ ]*" | cut -d' ' -f2) + # 使用更兼容的方式提取auth参数 + xauth_path=$(echo "$cmdline" | awk '/-auth/{print $2}' | head -1) if [ -n "$xauth_path" ]; then break fi fi done -# 如果没有找到,使用默认路径 +# 如果没有找到,检查常见的XAUTHORITY路径 if [ -z "$xauth_path" ]; then - xauth_path="/home/unitree/.Xauthority" - # 尝试更常见的gdm路径 + # 检查gdm路径 if [ -f "/run/user/1000/gdm/Xauthority" ]; then xauth_path="/run/user/1000/gdm/Xauthority" + # 检查lightdm路径 + elif [ -f "/var/run/lightdm/root/:0" ]; then + xauth_path="/var/run/lightdm/root/:0" + # 默认路径 + else + xauth_path="/home/unitree/.Xauthority" fi fi +export XAUTHORITY="$xauth_path" +echo "Using XAUTHORITY: $XAUTHORITY" + export XAUTHORITY="$xauth_path" {