TonyB TonyB - 1 year ago 79
Bash Question

bash script to reload browser if closes with error

I'm running a Raspberry Pi 2 on a screen to display a website. The Epiphany browser will sometimes close unexpectedly.

I'm trying to make a script that will reload the browser if it fails, the problem I'm having is there are xdotool commands that run afterwards that I cannot figure out where to place.

Here is what I have so far:

#!/bin/bash

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


This script runs, opens the browser, and will reload the browser if the process is killed. I need the following to run after it's started:

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


This makes epiphany go full screen

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


This refreshes the screen after a minute, and repeats every 60.

The reason why I don't have the web page itself refresh, is if there is a disruption in the network, it will blank out the screen with a Page not found and not retry again.

Thank you for reading.

Answer Source

Here is the solution I came up with in the end:

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

If you terminate the pid of epiphany, it will restart the browser on the next refresh cycle (30 seconds).

If you close the browser, it will exit the process.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download