You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SSH远程执行Shell脚本调用XServer命令失败的问题求助

SSH远程执行Shell脚本调用XServer命令失败的问题求助

我最近碰到个头疼的问题:想从树莓派上运行的XServer获取系统空闲时间值,在命令行直接敲DISPLAY=:0 xssstate -i完全正常,但把这个命令写到Shell脚本里后,本地运行脚本没问题,通过SSH远程执行就报错了。

我的脚本内容:

#!/bin/bash

# Wanted trigger timeout in milliseconds.
IDLE_TIME=$((10*1000))

triggered=0

while [ 1 -eq 1 ]
do
    sleep 1
    sstime=$(DISPLAY=:0 xssstate -i)
    echo "idletime $IDLE_TIME"
    echo "sstime $sstime"

    if [ "$sstime" -gt "$IDLE_TIME" ] || [ "$triggered" -eq 0 ]
    then
        echo 235 > /sys/waveshare/rpi_backlight/brightness
        triggered=1
    elif [ "$sstime" -lt "$IDLE_TIME" ] || [ "$triggered" -eq 1 ]
    then
        echo 0 > /sys/waveshare/rpi_backlight/brightness
        triggered=0
    else
        triggered=0
    fi
done

远程执行时的错误输出:

No protocol specified
Cannot open display.
idletime 10000
sstime
./backLTctrl.sh: line 15: [: : integer expression expected
No protocol specified
Cannot open display.
idletime 10000
sstime
./backLTctrl.sh: line 15: [: : integer expression expected
./backLTctrl.sh: line 19: [: : integer expression expected
No protocol specified
Cannot open display.
idletime 10000
sstime
./backLTctrl.sh: line 15: [: : integer expression expected
No protocol specified
Cannot open display.
idletime 10000
sstime
./backLTctrl.sh: line 15: [: : integer expression expected
./backLTctrl.sh: line 19: [: : integer expression expected

目前的测试情况:

  • 本地直接运行脚本:一切正常,能正确获取空闲时间并控制背光亮度
  • SSH远程执行脚本:就会出现上述错误,xssstate无法连接到XServer,导致后续的数值比较也因为空值报错

有没有大佬能帮忙分析下这是哪里出了问题,怎么解决远程执行时的XServer访问权限问题呀?

备注:内容来源于stack exchange,提问作者BDMan32

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 07:24:32