It seemed a problem with the “format” of the script file. I had to correct it using “dos2unix” command.
- Finallk, I created:
wireguard_watchdog.sh
script file in /storage/downloads of a CoreELEC Wireguard (Entware) client
with this content:
#!/bin/sh
tries=0
while [[ $tries -lt 3 ]]
do
if /bin/ping -c 1 10.1.1.1
then
date > /storage/downloads/wireguard_watchdog.log
echo WG wg0 working OK >> /storage/downloads/wireguard_watchdog.log
exit 0
fi
tries=$((tries+1))
done
wg-quick down wg0
sleep 4
wg-quick up wg0
date > /storage/downloads/wireguard_watchdog.log
echo WG wg0 restarted >> /storage/downloads/wireguard_watchdog.log
where:
10.1.1.1 is the WG server IP
/storage/downloads/wireguard_watchdog.log is a log file generated with the result of the process
-
I have moved the script to /storage/scripts, make it executable, AND CHANGE FORMAT (important, without this, the script does NOT work):
cp /storage/downloads/wireguard_watchdog.sh /storage/scripts
chmod +x /storage/scripts/wireguard_watchdog.sh
dos2unix /storage/scripts/wireguard_watchdog.sh -
After that, the script is fully functional, and you can execute it:
sh /storage/scripts/wireguard_watchdog.sh
and add it to crontab (every 2 minutes, for example):
crontab -e
add a line:
*/2 * * * * sh /storage/scripts/wireguard_watchdog.sh
I think this “closes” the circle for WG (Entware). It solves the DDNS of the server at init, and, when ping to the server fails, the script restart the WG connection.
There may be some more “elegant” solution, but I have very little scripting knowledge.
Kind regards