bash 关闭错误时重新加载浏览器的脚本

bash script to reload browser if closes with error

我在屏幕上 运行ning Raspberry Pi 2 以显示网站。 Epiphany 浏览器有时会意外关闭。

我正在尝试制作一个脚本,如果它失败会重新加载浏览器,我遇到的问题是有 xdotool 命令 运行 之后我不知道该放在哪里。

这是我目前的情况:

#!/bin/bash

until epiphany "http://localhost/index.php" ; do
echo "Service 'epiphany' crashed with exit code #?. Respawning..." >&2
sleep 1
done

此脚本 运行s,打开浏览器,如果进程被终止,将重新加载浏览器。启动后 运行 我需要以下内容:

sleep 10
xdotool search --class epiphany windowactivate
xdotool key F11

这让顿悟变得全屏

while ps ax | grep -v grep |epiphany ; do
sleep 60
echo "Refreshing page"
xdotool search --class epiphany windowactivate
xdotool key F5
done

这会在一分钟后刷新屏幕,并且每 60 次重复一次。

我没有让网页本身刷新的原因是,如果网络中断,它会黑屏并显示找不到页面并且不会重试。

感谢您的阅读。

您或许可以将它们组合成一个脚本。 这将是组合文件:

combined.sh:

#/bin/bash

epiphany "http://localhost/index.php" &
sleep 10
xdotool search --class epiphany windowactivate
xdotool key F11

while ps ax | grep -v grep |epiphany ; do
sleep 60
echo "Refreshing page"
xdotool search --class epiphany windowactivate
xdotool key F5
done

service.sh

#/bin/bash

until combined.sh ; do
echo "Service 'epiphany' crashed with exit code #?. Respawning..." >&2
sleep 1
done

我正在做这件事,没有要测试的覆盆子,但它应该可以工作。第一个脚本直到顿悟关闭才停止,当它关闭时 combined.sh 再次执行。

这是我最后想出的解决方案:

screen.sh

#!/bin/bash
if [ -a /home/pi/.config/epiphany/session_state.xml ];
     then
     rm /home/pi/.config/epiphany/session_state.xml;
fi

epiphany "http://localhost/index.php" &
sleep 10
xdotool search --desktop 0 --class epiphany-browser windowactivate
xdotool key F11

while ps ax |grep -v grep| grep epiphany; do
sleep 30
xdotool search --desktop 0 --class epiphany-browser windowactivate
xdotool key F5
done

web.sh

#!/bin/bash
/home/pi/screen.sh &
wait
until /home/pi/screen.sh; do
echo "Epiphany has closed in error, respawning..."
sleep 1
done

如果终止顿悟的pid,它将在下一个刷新周期(30秒)重新启动浏览器。

如果关闭浏览器,它将退出进程。

如果你想在没有其他工具的情况下运行 Ephiphany 最大化(非全屏)并在它崩溃时重新启动:

#!/bin/bash
#script that runs Ephipany browser and restarts it if it crashes.

WEBPAGEDIR=/home/pi/bin/WebPage
LOGFILE="$WEBPAGEDIR/WebPage_$(date '+%Y%m').log"

function log {
    LogStr="[$(date '+%F %X')] "
    echo "$LogStr"
    echo "$LogStr" >> "$LOGFILE"
}

function rmfile {
    rm "$WEBPAGEDIR/" &>/dev/null
}

function runBrowser {
    log "Starting browser"

    #-- delete old Epiphany session files so browser starts maximized
    rmfile states.xml
    rmfile session_state.xml~
    rmfile session_state.xml
    rmfile bookmarks.rdf
    rmfile cookies.sqlite
    rmfile ephy-*

    epiphany -a --profile="$WEBPAGEDIR" "$WEBPAGEDIR/index.html" &> /dev/null

    return $?
}

log "----------------"
log "New Session"


#-- start browser.  Restart it if it crashes --------- 
until runBrowser; do
    log "Browser crashed with exit code $?.  Restarting browser ..."
    sleep 1
done

# --- exit -----------------
log "Ended Session"
exit 0