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

树莓派Raspbian系统Shell脚本网页轮询失效问题排查与修改

问题描述

使用运行Raspbian系统的树莓派改造智能时钟,需求是开机打开3个网页并自动轮询。编写的Shell脚本仅能打开Outlook和Time.is网页,无法实现轮询功能,脚本如下:

# Open Outlook website and Time.is in fullscreen
chromium-browser --noerrdialogs --disable-session-crashed-bubble --disable-infobars --start-fullscreen https://outlook.office.com https://time.is

# List of websites to open (replace with your URLs)
websites=("https://app.datadoghq.eu")

# Iterate over the list of websites and open in new tabs
for website in "${websites[@]}"; do
    chromium-browser --new-tab "$website"
    sleep 5  # Adjust sleep time as needed
done

# Give Chromium some time to load tabs
sleep 10

# Infinite loop every 10 seconds
while true; do
    # Switch to the next tab using xdotool every 10 seconds
    xdotool key "ctrl+Tab"
    sleep 10

    # Check if the current tab is "Time.is" and stay on it for 1 minute
    current_tab_url=$(xdotool getwindowfocus getwindowname)
    if [[ $current_tab_url == *"https://time.is"* ]]; then
        sleep 50
    fi
    
    echo "Switching tabs every 10 seconds..."
    # Add your additional tasks here
done
问题原因分析
  • Chromium前台阻塞执行:第一个chromium-browser命令是前台运行的,会卡住脚本执行,后面打开第三个网页、轮询循环的代码根本不会运行。
  • 新标签打开方式错误:单独用chromium-browser --new-tab可能会启动新的Chromium实例,而非在已打开的窗口中添加标签,导致多个窗口无法统一轮询。
  • 窗口标题判断失效:Chromium的窗口标题显示的是网页的标题(比如Time.is的标题是"Time.is - 准确的世界时间"),而非URL,所以[[ $current_tab_url == *"https://time.is"* ]]的判断永远不成立,无法触发延长停留的逻辑。
  • 未确保Chromium窗口激活:xdotool key需要目标窗口处于激活状态,否则按键命令无法生效。
修改后的脚本
#!/bin/bash

# 后台启动Chromium并打开前两个网页,同时指定窗口名称方便后续定位
chromium-browser --noerrdialogs --disable-session-crashed-bubble --disable-infobars --start-fullscreen --app-window-name="SmartClock" https://outlook.office.com https://time.is &
CHROMIUM_PID=$!

# 等待Chromium窗口加载
sleep 8

# 在已打开的Chromium窗口中打开第三个网页(新标签)
xdotool search --name "SmartClock" windowactivate --sync key ctrl+t sleep 2 type "https://app.datadoghq.eu" key Return
sleep 5

# 预定义标签顺序和停留时间(索引从0开始,对应Outlook、Time.is、Datadog)
# 格式:标签索引:停留秒数
tab_timings=(
    "0:10"
    "1:60"
    "2:10"
)

# 无限轮询标签
while true; do
    for timing in "${tab_timings[@]}"; do
        # 解析标签索引和停留时间
        TAB_INDEX=$(echo "$timing" | cut -d: -f1)
        STAY_TIME=$(echo "$timing" | cut -d: -f2)
        
        # 切换到指定索引的标签(ctrl+数字键,注意索引+1,因为Chrome的ctrl+1对应第一个标签)
        xdotool search --name "SmartClock" windowactivate --sync key "ctrl+$((TAB_INDEX + 1))"
        
        # 停留指定时间
        sleep "$STAY_TIME"
        echo "Stayed on tab $TAB_INDEX for $STAY_TIME seconds, switching next..."
    done
done
修改说明
  • 后台运行Chromium:添加&让Chromium在后台运行,避免阻塞脚本后续代码,同时用--app-window-name给窗口命名,方便xdotool定位。
  • 统一窗口打开新标签:用xdotool定位到已打开的Chromium窗口,发送ctrl+t新建标签,再输入URL并回车,确保所有标签在同一个窗口中。
  • 基于标签索引轮询:放弃通过窗口标题判断,改为预定义标签顺序和停留时间,直接用ctrl+数字键切换标签,逻辑更可靠。
  • 确保窗口激活:每次切换标签前用xdotool search --name "SmartClock" windowactivate --sync激活目标窗口,保证按键命令生效。

内容的提问来源于stack exchange,提问作者Stefan Groenewald

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 00:43:13