-
Notifications
You must be signed in to change notification settings - Fork 0
/
cronwrapper.sh
executable file
·269 lines (227 loc) · 8.36 KB
/
cronwrapper.sh
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/bash
# ------------------------------------------------------------
#
# AXELS CRONWRAPPER
#
# ------------------------------------------------------------
#
# Call any command in a cronjob and write a parsable log.
#
# SYNTAX: SYNTAX: $0 TTL COMMAND [LABEL]
# Start script without parameters to see the help or have a
# to the ./docs/ directory.
#
# ------------------------------------------------------------
# 2002-02-06 ahahn V1.0
# 2002-07-15 Stderr wird auch ins Logfile geschrieben
# 2002-09-17 ahahn Email wird versendet, wenn Skript nicht
# ausfuehrbar ist.
# 2003-04-05 ahahn show output of executed script
# 2004-03-26 ahahn added output with labels 2 grab infos from output
# 2006-01-01 ahahn disabled email
# 2009-05-01 ahahn MPC: keinerlei Ausgabe auf stdout- Ausgabe nur im Log
# 2009-05-04 ahahn Test auf execute Rechte deaktiviert
# 2009-05-13 ahahn Check: Cron darf nur einmalig auf einem Server laufen
# Dies erfordert Umstellung der Parameter-Struktur
# 2009-05-14 ahahn sleep eingebaut mit Hilfe what_am_i
# 2009-05-18 ahahn mehr Infos zu Locking und ausfuehrendem Server im Output
# 2010-10-19 ahahn add JOBEXPIRE to output (to detect outdated cronjobs)
# 2012-04-03 ahahn Sourcen von $0.cfg fuer eigene Variablenwerte
# 2012-04-04 ahahn aktiver Job verwendet separates Logfile
# 2012-04-05 ahahn TTL mit in der Ausgabe
# 2012-04-13 ahahn joblog hinzugefuegt
# 2013-05-15 [email protected] FIRST IML VERSION
# 2013-07-xx [email protected] TTL ist max 1h TTL-Parameter-Wert
# 2013-08-07 [email protected] Strip html in der Ausgabe
# 2017-10-13 [email protected] use eval to execute multiple commands
# 2021-02-23 ahahn add help and parameter detection
# 2022-01-12 ahahn fixes based on shellcheck
# ------------------------------------------------------------
# show help
# param string info or error message
function showhelp(){
echo "
$line1
AXELS CRONWRAPPER
Puts control and comfort to cronjobs.
source: https://github.com/axelhahn/cronwrapper
license: GNU GPL 3.0
$line1
$1
SYNTAX: $0 TTL COMMAND [LABEL]
PARAMETERS:
TTL integer value in [min]
This value how often your cronjob runs. It is used to verify
if a cronjob is out of date / does not run anymore.
COMMAND command to execute
When using spaces or parameters then quote it.
Be strict: if your job is ok then exit wit returncode 0.
If an error occurs exit with returncode <> 0.
LABEL optional: label to be used as output filename
If not set it will be detected from basename of executed command.
When you start a script with different parameters it is highly
recommended to set the label.
REMARK:
You don't need to redirect the output in a cron config file. STDOUT and
STDERR will be fetched automaticly.
It also means: Generate as much output as you want and want to have to debug a
job in error cases.
OUTPUT:
The output directory of all jobs executed by $0 is
${LOGDIR}.
The output logs are parseble with simple grep command.
MONITORING:
You can run $(dirname $0)/cronstatus.sh to get a list of all cronjobs and its
status. Check its source. Based on its logic you can create a check script for
your server monitoring.
"
}
# helper function - append to file
function w() {
echo "$*" >>"$OUTFILE"
}
# ------------------------------------------------------------
# CONFIG
# ------------------------------------------------------------
# . `dirname $0`/config_allgemein.sh
line1="--------------------------------------------------------------------------------"
typeset -i TTL=$1 2>/dev/null
CALLSCRIPT=$2
LABELSTR=$3
LOGFILE=/tmp/call_any_script_$$.log
if [ "${LABELSTR}" = "" ]; then
LABELSTR=$(basename "${CALLSCRIPT}" | cut -f 1 -d " " )
fi
# Label darf keine Unterstriche enthalten
LABELSTR=$(echo ${LABELSTR} | sed "s#_#-#g")
TOUCHPART="_flag-${LABELSTR}_expire_"
LOGDIR="/var/tmp/cronlogs"
MYHOST=$( hostname -f )
# WHATAMI=/data/srdrs/admin/bin/what_am_i
JOBBLOGBASE=${MYHOST}_joblog_
# . $0.cfg
FINALOUTFILE="$LOGDIR/${MYHOST}_${LABELSTR}.log"
JOBLOG="$LOGDIR/${JOBBLOGBASE}$(date +%a).done"
# OUTFILE="$LOGDIR/`hostname`_${LABELSTR}.log"
OUTFILE="$FINALOUTFILE.running"
typeset -i iStart
iStart=$(date +%s)
# ------------------------------------------------------------
# CHECK PARAMS
# ------------------------------------------------------------
if [ "$1" = "-?" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
showhelp "Showing help ..."
exit 1
fi
if [ $# -lt 2 ]; then
showhelp "ERROR: missing parameters."
exit 1
fi
if [ $TTL -eq 0 ]; then
showhelp "ERROR: TTL must be integer and greater zero."
exit 1
fi
if [ -z "${MYHOST}" ]; then
showhelp "ERROR: hostname -f did not return any hostname."
exit 1
fi
# ------------------------------------------------------------
# WRITE HEADER
# ------------------------------------------------------------
mkdir $LOGDIR 2>/dev/null
chmod 777 $LOGDIR 2>/dev/null
rm -f "$OUTFILE" 2>/dev/null
touch "$OUTFILE"
w "REM $line1"
w "REM CRON WRAPPER - $MYHOST"
w "REM $line1"
w "SCRIPTNAME=${CALLSCRIPT}"
w "SCRIPTTTL=${TTL}"
w "SCRIPTSTARTTIME=$( date '+%Y-%m-%d %H:%M:%S' ), $iStart"
w "SCRIPTLABEL=${LABELSTR}"
if [ -z "${CALLSCRIPT}" ]; then
w "REM STOP: no script was found. check syntax for $(basename $0)"
exit 1
fi
# ------------------------------------------------------------
# CHECK: runs this job on another machine?
# ------------------------------------------------------------
w REM $line1
typeset -i iExpire
iExpire=$(date +%s)
typeset -i iExpDelta=$(( TTL*3/2 ))
if [ $iExpDelta -gt 60 ]; then
iExpDelta=60
fi
# let iExpire=$iExpire+$TTL*60*3/2
iExpire=$(( iExpire+TTL*60 + iExpDelta*60 ))
if [ $TTL -eq 0 ]; then
iExpire=0
fi
aLastfiles=( "${LOGDIR}"/*"${TOUCHPART}"* )
lastfile=${aLastfiles[0]}
if ls "${lastfile}" >/dev/null 2>&1; then
TOUCHFILE=$(basename "$lastfile")
typeset -i expdate
expdate=$(echo "$TOUCHFILE"| cut -f 4 -d "_") 2>/dev/null
runserver=$(echo "$TOUCHFILE" | cut -f 5 -d "_")
w "REM INFO: expires $expdate - $(date -d @$expdate)"
typeset -i timeleft=$expdate-$iStart
w "REM INFO: job is locked for other servers for $timeleft more seconds"
if ! hostname | grep -F "$runserver" >/dev/null; then
w "REM INFO: it locked up to $expdate by $runserver"
if [ $timeleft -gt 0 ]; then
w REM STOP: job is locked.
mv "$OUTFILE" "${FINALOUTFILE}"
exit 2
else
w REM INFO: OK, job is expired
fi
else
w REM INFO: job was executed on the same machine and can be executed here again.
fi
else
w REM OK, executing job the first time
fi
# -- delete all touchfiles of this job
rm -f "${LOGDIR}"/*"${TOUCHPART}"* 2>/dev/null
# -- create touchfile for this server
touch "${LOGDIR}/${TOUCHPART}${iExpire}_${MYHOST}"
w JOBEXPIRE=${iExpire}
# w REM INFO: created touchfile ${TOUCHPART}${iExpire}_`hostname`
w REM $line1
# ------------------------------------------------------------
# MAIN
# ------------------------------------------------------------
rc=none
# RETSTATUS="OK"
eval "${CALLSCRIPT}" >"${LOGFILE}" 2>&1
rc=$?
# if [ $rc -ne 0 ]; then
# RETSTATUS="WARNING !!!"
# fi
# w "sending email..."
# cat "${LOGFILE}" | mail -s"${EMAIL_SUBJECT} - ${LABELSTR} - $RETSTATUS" "${EMAIL_TO}"
# w " rc=$?"
typeset -i iEnd
iEnd=$(date +%s)
w "SCRIPTENDTIME=$( date '+%Y-%m-%d %H:%M:%S' ), $iEnd"
iExectime=$(( iEnd-iStart ))
w SCRIPTEXECTIME=$iExectime s
w SCRIPTRC=$rc
w "REM $line1"
sed -e 's/<[^>]*>//g' "${LOGFILE}" | sed "s#^#SCRIPTOUT=#g" >>"$OUTFILE"
w "REM $line1"
# write a log for execution of a cronjob
echo "job=${LABELSTR}:host=$MYHOST:start=$iStart:end=$iEnd:exectime=$iExectime:ttl=${TTL}:rc=$rc" >>"$JOBLOG"
chmod 777 "$JOBLOG" 2>/dev/null
find $LOGDIR -name "${JOBBLOGBASE}*" -type f -mtime +4 -exec rm -f {} \;
# ------------------------------------------------------------
# CLEANUP UND ENDE
# ------------------------------------------------------------
rm -f "${LOGFILE}"
w "REM $0 finished at $(date)"
mv "${OUTFILE}" "${FINALOUTFILE}"
# ------------------------------------------------------------
# EOF
# ------------------------------------------------------------