+bin/shutdown-detect*
authorJan Kratochvil <jan@jankratochvil.net>
Sat, 19 Jun 2021 19:51:55 +0000 (21:51 +0200)
committerJan Kratochvil <jan@jankratochvil.net>
Sat, 19 Jun 2021 19:51:55 +0000 (21:51 +0200)
bin/shutdown-detect [new file with mode: 0755]
bin/shutdown-detect-boot [new file with mode: 0755]
bin/shutdown-detect.service [new file with mode: 0644]

diff --git a/bin/shutdown-detect b/bin/shutdown-detect
new file mode 100755 (executable)
index 0000000..53bc406
--- /dev/null
@@ -0,0 +1,4 @@
+#! /bin/bash
+echo "$(date --iso=seconds) shutdown $* boot=$(uptime -s) $(uptime -p)" >>/var/log/shutdown-detect.log
+touch /var/log/shutdown-detect.ok
+sync
diff --git a/bin/shutdown-detect-boot b/bin/shutdown-detect-boot
new file mode 100755 (executable)
index 0000000..7990229
--- /dev/null
@@ -0,0 +1,11 @@
+#! /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
diff --git a/bin/shutdown-detect.service b/bin/shutdown-detect.service
new file mode 100644 (file)
index 0000000..88cd5a1
--- /dev/null
@@ -0,0 +1,15 @@
+# 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