aboutsummaryrefslogtreecommitdiff
path: root/src/infrastructure/scripts/cronjob.sh
blob: 8abe510c6c18ec595b1b185e871d430c0ec7a188 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/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



epoch() {
	awk 'BEGIN { srand(); print(srand()); }'
}

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() {
	# Same as:
	# sed -u "s|^|[$CMD]: |"
	# but the "-u" option is not POSIX
	IFS=''
	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="$*"
HOSTNAME="$(hostname)"
FROM="cronjob@$HOSTNAME"
TIMEOUT='14400' # four hours
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: $CMD
		Starting at: $(now)

	EOF

	START="$(epoch)"
	STATUS=0
	timeout "$TIMEOUT" "$@" || 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" >> /var/log/cronjobs.log