robot_face_rec/start.sh

80 lines
2.0 KiB
Bash
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
# --- 日志相关 ---
LOG_DIR="/home/unitree/robot_face_rec/logs/face_rec"
mkdir -p "$LOG_DIR"
LOG_FILE="$LOG_DIR/face_rec_$(date +%Y%m%d_%H%M%S).log"
{
echo "===== $(date) Starting face_rec script ====="
echo "User: $(whoami)"
echo "PWD before cd: $(pwd)"
echo "Python: $(which python) / Version: $(python --version 2>&1)"
} >> "$LOG_FILE"
# 切换到项目目录
cd /home/unitree/robot_face_rec || {
echo "Failed to cd to project dir" >> "$LOG_FILE"
exit 1
}
{
echo "PWD after cd: $(pwd)"
echo "Env before display detection: DISPLAY=$DISPLAY, XAUTHORITY=$XAUTHORITY"
} >> "$LOG_FILE"
# --- DISPLAY 设置逻辑 ---
# 使用默认DISPLAY值:0这是Linux系统中最常见的显示编号
display_found=":0"
echo "Using default DISPLAY: :0"
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则尝试 fallback
if [ -z "$display_found" ]; then
# 常见默认::0
display_found=":0"
echo "Chosen default DISPLAY: :0"
fi
export DISPLAY="$display_found"
# 从Xorg命令行参数中获取正确的XAUTHORITY路径
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)
if [ -n "$xauth_path" ]; then
break
fi
fi
done
# 如果没有找到,使用默认路径
if [ -z "$xauth_path" ]; then
xauth_path="/home/unitree/.Xauthority"
# 尝试更常见的gdm路径
if [ -f "/run/user/1000/gdm/Xauthority" ]; then
xauth_path="/run/user/1000/gdm/Xauthority"
fi
fi
export XAUTHORITY="$xauth_path"
{
echo "Chosen DISPLAY = $DISPLAY"
echo "Using XAUTHORITY = $XAUTHORITY"
} >> "$LOG_FILE"
# 最终执行 Python 程序(不后台化,用 exec 替换进程)
exec python face_rec.py >> "$LOG_FILE" 2>&1