--- /dev/null
+#! /bin/bash
+echo "$(date --iso=seconds) shutdown $* boot=$(uptime -s) $(uptime -p)" >>/var/log/shutdown-detect.log
+touch /var/log/shutdown-detect.ok
+sync
--- /dev/null
+#! /bin/bash
+boot="boot $* boot=$(uptime -s) $(uptime -p)"
+echo "$(date --iso=seconds) uninit-time $boot" >>/var/log/shutdown-detect.log
+# Raspberry Pi 3B+: 15 sec
+sleep 60
+echo "$(date --iso=seconds) init-time $boot" >>/var/log/shutdown-detect.log
+if [ -e /var/log/shutdown-detect.ok ];then
+ rm -f /var/log/shutdown-detect.ok
+ exit 0
+fi
+echo "$(date --iso=seconds) unclean shutdown"|tee -a /var/log/shutdown-detect.log|mail -s "unclean shutdown: $(hostname)" jan@jankratochvil.net
--- /dev/null
+# jankratochvil:
+# ln -s ../../../root/bin/shutdown-detect.service /etc/systemd/system/shutdown-detect.service; systemctl enable shutdown-detect.service
+# https://unix.stackexchange.com/a/41756/296319
+
+[Unit]
+Description=shutdown-detect
+
+[Service]
+Type=oneshot
+RemainAfterExit=true
+ExecStart=/root/bin/shutdown-detect-boot
+ExecStop=/root/bin/shutdown-detect
+
+[Install]
+WantedBy=multi-user.target