diff options
Diffstat (limited to 'src/infrastructure/scripts/cronjob.sh')
-rwxr-xr-x | src/infrastructure/scripts/cronjob.sh | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/src/infrastructure/scripts/cronjob.sh b/src/infrastructure/scripts/cronjob.sh new file mode 100755 index 0000000..4823ac1 --- /dev/null +++ b/src/infrastructure/scripts/cronjob.sh @@ -0,0 +1,159 @@ +#!/bin/sh +set -eu + +usage() { + cat <<-'EOF' + Usage: + cronjob COMMAND... + cronjob -h + EOF +} + +help() { + cat <<-'EOF' + + + Options: + -h, --help show this message + + COMMAND the command to be executed + + + Execute the given command, and send the output to email, with + special treatment to the status code. It kills the job it it + lasts more than one hour. + + It load the appropriate files, so that the actual cron + declaration is smaller. + + + Examples: + + Run a backup: + + $ cronjob backup -q cron + EOF +} + + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 'h' flag; do + case "$flag" in + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + +if [ -z "${1:-}" ]; then + printf 'Missing COMMAND.\n\n' >&2 + usage >&2 + exit 2 +fi + +if [ "$(id -un)" != 'root' ]; then + printf 'This script must be run as root.\n\n' >&2 + usage >&2 + exit 2 +fi + + +set +eu +# shellcheck source=/dev/null +. /etc/rc +set -eu + + + +now() { + date '+%Y-%m-%dT%H:%M:%S%:z' +} + + +uuid() { + od -xN20 /dev/random | + head -n1 | + awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}' +} + +mkstemp() { + name="${TMPDIR:-/tmp}/uuid-tmpname with spaces.$(uuid)" + touch "$name" + printf '%s' "$name" +} + +pre() { + sed -u "s|^|[$CMD]: |" +} + +duration() { + minutes=$((${1} / 60)) + seconds=$((${1} % 60)) + printf '%sm%ss' "$minutes" "$seconds" +} + + +CMD="$*" +HOSTNAME="$(hostname)" +FROM="cronjob@$HOSTNAME" +ONE_HOUR='3600' +STATUS_F="$(mkstemp)" +OUT="$(mkstemp)" + +email() { + { + cat <<-EOF + Content-Type: text/plain; charset=UTF-8 + Content-Transfer-Encoding: 8bit + From: $FROM + To: root@localhost + Subject: (exit status: $(cat "$STATUS_F")) - $HOSTNAME: $CMD + + EOF + cat "$OUT" + } | sendmail -t -f "$FROM" + rm -f "$OUT" "$STATUS_F" +} +trap email EXIT + +{ + cat <<-EOF + Running commad: $* + Starting at: $(now) + + EOF + + START="$(date +%s)" + STATUS=0 + timeout "$ONE_HOUR" "$@" || STATUS=$? + printf '%s' "$STATUS" > "$STATUS_F" + END="$(date +%s)" + 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" >> /var/log/cronjobs.log |