Hi
I have a remote headless Raspi to receive my local daily Duplicati backup roughly around 3A.M. then shutdown.
I wrote a script for this and found one of the tests could provide an unintended bonus, that is keep the PI awake if I’d ever need to access it. Of course, as the PI can’t wake with WoL I’d have to wait the next AutoWakeUp (hardware external clock, driven by the PI to shutdown itself).
My problem is with an until
loop that tests and sleep 5
if my Home instance of Synthing is not connected. E.g yesterday ~22h I “Paused” the PI device in my Home ST, hoping this morning I could work with the PI (ssh). Unfortunately it is shutdown.
I guess ...rest/system/connections...device "connected": true,
is not reliable enough for my script (kiddy skilled) because the syslog show every minute 3 lines (redacted):
Timestamp [PI_ID] Time INFO: Established secure connection to HomeID at PI_IP:Port-Home_IP:Port/tcp-client/TLS1.3-TLS_AES_128_GCM_SHA256
Timestamp [PI_ID] Time INFO: Device Home_ID client is "syncthing v1.5.0" named "Home" at PI_IP:Port-Home_IP:Port/tcp-client/TLS1.3-TLS_AES_128_GCM_SHA256
Timestamp [PI_ID] Time INFO: Connection to Home_ID at PI_IP:Port-Home_IP:Port/tcp-client/TLS1.3-TLS_AES_128_GCM_SHA256 closed: reading length: EOF
Would these may defeat my test if it runs at the very moment my Home device would briefly appear as connected for a very short while? Here is my script:
#!/bin/bash
# AutoOff.sh
#Script to detect lack of ST traffic then shutdown Raspberry Pi.
#Do not move, or change scheduler settings (see below) accordingly.
#Detection rate is driven by cron. Set with "sudo crontab -e -u root"
#which edits /var/spool/cron/crontabs/root.
#No need more than one Myself instancy as long as I'm well conceived :-)
if [ `pgrep -c AutoOff.sh` -gt 1 ]; then exit 0
fi
#Give some time to ST to start if it just started before me:
while [ `pgrep -c syncthing` -lt 2 ]; do
sleep 1
done
#Wait for Home ST to be connected
Conf="/home/pi/.config/syncthing/config.xml"
Port=$(awk -F'[:<]' '/<gui en/,/gui>/{if($0~/address/){print$3}}' $Conf)
APIKey=$(awk -F'[><]' '/apikey/{print $3}' $Conf)
REST="curl -ksH X-API-Key:$APIKey https://localhost:$Port/rest"
HomeID=$(awk -F\" '/Home/{print $2}' $Conf)
until $REST/system/connections|awk -v HID=$HomeID '$0~HID,/}/ {if ($0~/connected/) print}'|grep -q '"connected": true,'; do
sleep 5
done
#Give time for Index exchange
sleep 2
#Exit if someone is logged on with SSH and come back next quater hour (cron).
if [ `pgrep -c sshd` -gt 1 ]; then exit 0
fi
#Detects if ST needs new files:
needF=`$REST/db/status?folder=folder-ID|grep -sm1 needFiles|grep -Eso '[0-9]+'`
#if not, shutdown PI:
if [ $needF -eq 0 ]; then echo `date` AutoOff.sh GoToBed >> /var/log/syslog
/sbin/shutdown #>/dev/null 2>&1
fi
Thanks for help