如何重启已关闭的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-fullscreenflag:
Note: If your Pi useschromium-browser --start-fullscreenchromiuminstead ofchromium-browser, swap the command accordingly. - Openbox GUI Restart: Press
Alt+F2to 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:
- 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 - Make it executable:
chmod +x ~/chromium_keeper.sh - Add it to Openbox's autostart to run on boot:
Edit~/.config/openbox/autostartand 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:
- Create a systemd service file:
sudo nano /etc/systemd/system/chromium.service - Paste this content (adjust
Userif your username isn'tpi):[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 - 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):
- First install
wmctrl:sudo apt update && sudo apt install wmctrl - 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 - Make it executable and add to Openbox autostart:
Add this line tochmod +x ~/chromium_monitor.sh~/.config/openbox/autostart:bash ~/chromium_monitor.sh &
Quick Notes:
- If
chromium-browserdoesn't work, replace it withchromium(check withwhich chromium). - Run
wmctrl -lxwhile Chromium is open to confirm the exact window class if the script doesn't detect minimized windows.
内容的提问来源于stack exchange,提问作者danfs
相关产品推荐
相关产品推荐

