aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/with-email92
-rwxr-xr-xetc/sh/cronjob.sh62
2 files changed, 60 insertions, 94 deletions
diff --git a/bin/with-email b/bin/with-email
deleted file mode 100755
index 02789e9..0000000
--- a/bin/with-email
+++ /dev/null
@@ -1,92 +0,0 @@
-#!/bin/sh
-set -eu
-
-usage() {
- cat <<-'EOF'
- Usage:
- with-email [-s SUBJECT] COMMAND...
- with-email -h
- EOF
-}
-
-help() {
- cat <<-'EOF'
-
- Options:
- -s SUBJECT set the subject of the email
- -h, --help show this message
-
- COMMAND the command to be wrapped
-
-
- Executes COMMAND and send all of its output via email
- to eu@euandre.org.
-
-
- Examples:
-
- Run a script and use the default subject:
-
- $ with-email -- ./script
-
- Run a command and use a custom subject:
-
- $ with-email -s 'CRONJOB' echo 123
- EOF
-}
-
-for flag in "$@"; do
- case "$flag" in
- --)
- break
- ;;
- --help)
- usage
- help
- exit
- ;;
- *)
- ;;
- esac
-done
-
-SUBJECT='NO SUBJECT'
-while getopts 's:h' flag; do
- case "$flag" in
- s)
- SUBJECT="$OPTARG"
- ;;
- h)
- usage
- help
- exit
- ;;
- *)
- usage >&2
- exit 2
- ;;
- esac
-done
-shift $((OPTIND - 1))
-
-eval "$(assert-arg -- "${1:-}" 'COMMAND...')"
-
-now() {
- date '+%Y-%m-%dT%H:%M:%S%Z'
-}
-
-OUT="$(mkstemp)"
-trap 'rm -f "$OUT"' EXIT
-{
- printf 'Running command: %s\n' "$*"
- printf 'Starting at: %s\n' "$(now)"
- printf '\n'
-
- STATUS=0
- "$@" || STATUS=$?
-
- printf '\n'
- printf '\nFinished at: %s\n' "$(now)"
-} 1>"$OUT" 2>&1
-
-email -s "(exit status: $STATUS) - $SUBJECT" eu@euandre.org < "$OUT"
diff --git a/etc/sh/cronjob.sh b/etc/sh/cronjob.sh
index 31e7b28..5a2eb70 100755
--- a/etc/sh/cronjob.sh
+++ b/etc/sh/cronjob.sh
@@ -66,9 +66,67 @@ shift $((OPTIND - 1))
set +e
-# shellcheck disable=1090
+# shellcheck source=/dev/null
. ~/.profile
set -e
+
+epoch() {
+ awk 'BEGIN { srand(); print(srand()) }'
+}
+
+now() {
+ date '+%Y-%m-%dT%H:%M:%S%:z'
+}
+
+pre() {
+ # Same as:
+ # sed -u "s|^|[$CMD]: |"
+ # but the "-u" option is not POSIX
+ while read -r line; do
+ printf '[%s]: %s\n' "$CMD" "$line"
+ done
+}
+
+duration() {
+ minutes=$((${1} / 60))
+ seconds=$((${1} % 60))
+ printf '%sm%ss' "$minutes" "$seconds"
+}
+
+
CMD="$*"
-with-email -s "$(hostname): $CMD" -- nice -n15 "$@" 1>>"$XDG_LOG_HOME"/euandreh/mcron.log 2>&1
+HOSTNAME="$(hostname)"
+ONE_HOUR='3600'
+STATUS_F="$(mkstemp)"
+OUT="$(mkstemp)"
+
+send_email() {
+ FROM="cronjob@$HOSTNAME"
+ TO='root@localhost'
+ SUBJECT="(exit status: $(cat "$STATUS_F")) - $HOSTNAME: $CMD"
+ email -s "$SUBJECT" -f "$FROM" "$TO" < "$OUT"
+ rm -f "$OUT" "$STATUS_F"
+}
+trap send_email EXIT
+
+{
+ cat <<-EOF
+ Running command: $CMD
+ Start at: $(now)
+
+ EOF
+
+ START="$(epoch)"
+ STATUS=0
+ timeout "$ONE_HOUR" "$@" || STATUS=$?
+ printf '%s' "$STATUS" > "$STATUS_F"
+ END="$(epoch)"
+ DURATION_SECONDS=$((END - START))
+
+ cat <<-EOF
+
+ Finished at: $(now)
+ Duration: $(duration "$DURATION_SECONDS")
+ EOF
+} 2>&1 | pre | ts '%Y-%m-%dT%H:%M:%S' | tee "$OUT" >> "$XDG_LOG_HOME"/cronjobs.log