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

如何重启已关闭的Chromium?树莓派场景下后台自动重启实现

Hey there! Let's tackle your two Chromium-related questions for your Raspberry Pi setup with Openbox:

1. How to Restart a Closed Chromium Instance

Restarting a closed Chromium instance is straightforward, depending on how you want to do it:

  • Manual Terminal Restart: Open a terminal and run the base command for Chromium. If you want it to launch in fullscreen (matching your auto-start setup), add the --start-fullscreen flag:
    chromium-browser --start-fullscreen
    
    Note: If your Pi uses chromium instead of chromium-browser, swap the command accordingly.
  • Openbox GUI Restart: Press Alt+F2 to open the run dialog, type the command above, and hit Enter. If you've configured Openbox's right-click menu, you might also find Chromium listed under the "Internet" section.
2. Automatically Restart Chromium When Closed or Minimized

This is totally achievable with scripts or systemd. Let's break it down by scenario:

Handling Closed Chromium (Process Terminated)

Option 1: Simple Bash Loop Script

This script checks at regular intervals if Chromium is running, and restarts it if it's not:

  1. Create a script file (e.g., ~/chromium_keeper.sh):
    #!/bin/bash
    CHROMIUM_CMD="chromium-browser --start-fullscreen"
    CHROMIUM_PROCESS="chromium-browser"
    
    while true; do
        # Check if Chromium process exists
        if ! pgrep -x "$CHROMIUM_PROCESS" > /dev/null; then
            echo "Chromium closed, restarting..."
            $CHROMIUM_CMD &
        fi
        # Wait 5 seconds before next check (adjust as needed)
        sleep 5
    done
    
  2. Make it executable:
    chmod +x ~/chromium_keeper.sh
    
  3. Add it to Openbox's autostart to run on boot:
    Edit ~/.config/openbox/autostart and add this line at the end:
    bash ~/chromium_keeper.sh &
    

Option 2: Systemd Service (More Reliable)

Systemd is built for keeping processes running automatically, even after reboots:

  1. Create a systemd service file:
    sudo nano /etc/systemd/system/chromium.service
    
  2. Paste this content (adjust User if your username isn't pi):
    [Unit]
    Description=Persistent Chromium Instance
    After=graphical.target
    
    [Service]
    User=pi
    ExecStart=/usr/bin/chromium-browser --start-fullscreen
    Restart=always  # Restart on any exit
    RestartSec=5    # Wait 5 seconds before restarting
    Environment=DISPLAY=:0  # Target the main display
    
    [Install]
    WantedBy=graphical.target
    
  3. Enable and start the service:
    sudo systemctl enable chromium.service
    sudo systemctl start chromium.service
    

Handling Minimized Chromium

To detect minimized windows, we'll use wmctrl (a window management tool):

  1. First install wmctrl:
    sudo apt update && sudo apt install wmctrl
    
  2. Create an enhanced monitoring script (e.g., ~/chromium_monitor.sh):
    #!/bin/bash
    CHROMIUM_CMD="chromium-browser --start-fullscreen"
    CHROMIUM_PROCESS="chromium-browser"
    # Verify the window class with `wmctrl -lx` if this doesn't work
    CHROMIUM_CLASS="chromium-browser.Chromium"
    
    while true; do
        if pgrep -x "$CHROMIUM_PROCESS" > /dev/null; then
            # Get the Chromium window ID
            WINDOW_ID=$(wmctrl -lx | grep "$CHROMIUM_CLASS" | awk '{print $1}')
            if [ -n "$WINDOW_ID" ]; then
                # Check if window is minimized (state code 1 = hidden)
                WINDOW_STATE=$(wmctrl -lG | grep "$WINDOW_ID" | awk '{print $5}')
                if [ "$WINDOW_STATE" -eq 1 ]; then
                    echo "Chromium minimized, restoring it..."
                    # Option 1: Restore the window without restarting
                    wmctrl -i -r "$WINDOW_ID" -b remove,hidden
                    # Option 2: Restart Chromium (uncomment below if preferred)
                    # pkill "$CHROMIUM_PROCESS"
                    # $CHROMIUM_CMD &
                fi
            fi
        else
            echo "Chromium closed, restarting..."
            $CHROMIUM_CMD &
        fi
        sleep 3  # Check every 3 seconds
    done
    
  3. Make it executable and add to Openbox autostart:
    chmod +x ~/chromium_monitor.sh
    
    Add this line to ~/.config/openbox/autostart:
    bash ~/chromium_monitor.sh &
    

Quick Notes:

  • If chromium-browser doesn't work, replace it with chromium (check with which chromium).
  • Run wmctrl -lx while Chromium is open to confirm the exact window class if the script doesn't detect minimized windows.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 15:42:56