]> git.ekhem.eu.org Git - cron.git/commitdiff
Implement a service to notify about failures with an email.
authorJakub Czajka <jakub@ekhem.eu.org>
Wed, 29 Nov 2023 22:57:04 +0000 (23:57 +0100)
committerJakub Czajka <jczajka@google.com>
Sun, 24 Dec 2023 18:34:02 +0000 (19:34 +0100)
notify_failure@.service [new file with mode: 0644]
notify_on_failure.sh [new file with mode: 0755]

diff --git a/notify_failure@.service b/notify_failure@.service
new file mode 100644 (file)
index 0000000..4a67097
--- /dev/null
@@ -0,0 +1,16 @@
+# Copyright (c) 2023 Jakub Czajka <jakub@ekhem.eu.org>
+# License: GPL-3.0 or later.
+
+[Unit]
+Description=Notify about failure through an email notification.
+
+[Service]
+Type=oneshot
+ExecStartPre=/usr/bin/git \
+    --work-tree=/tmp \
+    --git-dir=/srv/git/cron.git \
+    checkout main --force notify_on_failure.sh
+ExecStart=/tmp/notify_on_failure.sh %I
+
+[Install]
+WantedBy=multi-user.target
diff --git a/notify_on_failure.sh b/notify_on_failure.sh
new file mode 100755 (executable)
index 0000000..2043dc7
--- /dev/null
@@ -0,0 +1,19 @@
+#!/bin/sh
+# Copyright (c) 2023 Jakub Czajka <jakub@ekhem.eu.org>
+# License: GPL-3.0 or later.
+
+MAILTO="server@${private_domain}"
+MAILFROM="server@${private_domain}"
+UNIT=${1}
+
+/usr/sbin/sendmail ${MAILTO} <<EOF
+From:${MAILFROM}
+To:${MAILTO}
+Subject:Status mail for unit: ${UNIT}
+
+$(journalctl --unit=${UNIT} \
+             --since="10 minute ago" \
+             --output=short-iso \
+             --no-hostname \
+             --no-pager)
+EOF