-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonce-upon-a-time.sh
executable file
·195 lines (150 loc) · 6.32 KB
/
once-upon-a-time.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
#!/bin/bash
# TODO manage trap TERM/INT
SELF="${BASH_SOURCE[0]##*/}"
NAME="${SELF%.sh}"
failedDir="/var/tmp/$NAME"
lockDir="$failedDir"
logDir="/var/log/crons"
OPTS="cCeEFhlLstw:W:xZ"
USAGE="Usage: $SELF [$OPTS] [--] <cmdline> [arg] [...]"
HELP="
$USAGE
-c clean failedFile on success (== lock does not exist)
-C clean failedFile if return code == 0
-e bash set -e
-E prompt on error
-F create failed file if return code != 0
-h this help
-l log to $logDir/<cmdline>.log (default: stdout, stderr)
-L log append to $logDir/<cmdline>.log (default: stdout, stderr)
-s simul
-t simul
-w <s> warn if execution time < s
-W <s> warn if execution time > s
-x bash set -x
-Z clean lock, failedFile and exit, usefull incase of stalled lock, use with caution
if <cmdline> [arg] [...] is NOT running, run it
else create a .failed file in $workingDir and exit
envvar: ONCE_UPON_A_{LOCK,LOG,FAILED}_DIR && ONCE_UPON_A_{LOCK,LOG,FAILED}_FILE
ex: export ONCE_UPON_A_LOCK_FILE=sleep-any-arg
$SELF sleep 1m &
$SELF sleep 2m &
"
function _log ()
{
[[ -t 2 ]] && echo -e "$@" >&2 || $run logger -t "$SELF[$$]" -- "$*"
}
function _quit ()
{
_log "$@"
exit 1
}
# only usefull in run=echo mode
function _create_file ()
{
local file="$1" content="$2"
# in a subshell, don't allow bash to overwrite file, and try to create/overwrite $file
# redirect stderr to /dev/null
( set -o noclobber; echo -n "$content" > $file ) 2> /dev/null
}
function _remove_file ()
{
local file="$1"
$run rm -f "$file"
}
unset run setX setE minWarn maxWarn doLog deleteFailedFileOnSuccess createFailedFileIfNotZero deleteFailedFileOnZero deleteLockAndExit
while getopts :$OPTS arg
do
case "$arg" in
c) deleteFailedFileOnSuccess=1 ;;
C) deleteFailedFileOnZero=1 ;;
e) setE="set -e" ;;
E) trap "read -p 'an error occurred, press ENTER '" ERR ;;
F) createFailedFileIfNotZero=1 ;;
h) _quit "$HELP" ;;
l) doLog=1 ;;
L) doLog=2 ;;
s) run=echo ;;
t) run=echo ;;
w) minWarn="$OPTARG" ;;
W) maxWarn="$OPTARG" ;;
x) setX="set -x" ;;
Z) deleteLockAndExit=1 ;;
:) _quit "$SELF: option -$OPTARG needs an argument." ;;
*) _quit " $USAGE" ;;
esac
done
shift $(($OPTIND - 1))
$setE
$setX
[[ $# -eq 0 ]] && _quit " $USAGE"
cmdLineStr="$*"
cmdLineArr=("$@")
command -v "${cmdLineArr[0]}" >/dev/null || _quit "$SELF: ${cmdLineArr[0]}: No such file or not executable."
[[ "$minWarn" == *[![:digit:]]* ]] && _quit "$SELF: $minWarn: Invalid number"
[[ "$maxWarn" == *[![:digit:]]* ]] && _quit "$SELF: $maxWarn: Invalid number"
# get envVar
for i in failed lock log
do
varDirName="${i}Dir" envDirName="ONCE_UPON_A_${i^^}_DIR"
envDirValue="${!envDirName}" varDirValue="${envDirValue:-$ONCE_UPON_A_DIR}" varDirValue="${varDirValue:-${!varDirName}}"
declare "$varDirName=$varDirValue"
varFileName="${i}File" envFileName="ONCE_UPON_A_${i^^}_FILE"
envFileValue="${!envFileName}" varFileValue="${envFileValue:-$ONCE_UPON_A_FILE}" varFileValue="${varFileValue:-$cmdLineStr}"
varFileValue="${varFileValue//[\/[:blank:]]/_}"
declare "$varFileName=$varDirValue/$varFileValue.${i}"
done
if (( deleteLockAndExit ))
then
$run _remove_file $lockDir
$run _remove_file $failedFile
exit 0
fi
# create $workingDir
$run mkdir -m 1777 -p "$failedDir" "$lockDir" || _quit "$SELF: Can't create '$failedDir' or '$lockDir'"
# if we can't create $lockFile with $cmdLineStr in it
$run _create_file "$lockFile" "$cmdLineStr"
# it will return a code != 0
createLockFileReturnCode=$?
if (( createLockFileReturnCode ))
then
# FIXME script.sh will appear as "bash script.sh" in /bin/ps and won't be catched by pgrep
if ! pgrep -fx "$cmdLineStr" > /dev/null
then
$run _create_file "$failedFile" "Warning: '$cmdLineStr' is not running, removing stalled lock"
_remove_file "$lockFile"
_quit "Warning: '$cmdLineStr' is not running, stalled lock removed"
# TODO: save original command line with option and run it below
_log "Warning: '$cmdLineStr' is not running, stalled lock removed"
$run _create_file "$failedFile" "Warning: '$cmdLineStr' is not running, stalled lock removed"
exec $originalCommandLineWithOptions "$@"
else
# create a failedFile
$run _create_file "$failedFile" "$cmdLineStr: lockfile exists."
_quit "$SELF: already running: $cmdLineStr (lockFile: $lockFile)"
fi
fi
# delete failedFile on succes
(( deleteFailedFileOnSuccess )) && $run _remove_file $failedFile
# remove lockFile on exit, and exit with $commandLineReturnCode
trap '_remove_file "$lockFile"; exit $commandLineReturnCode' EXIT
# log stdout and stderr in $logFile
(( doLog == 1 )) && $run exec &> $logFile
(( doLog == 2 )) && $run exec &>> $logFile
# flooding syslog is always a pleasure
_log "starting: '$cmdLineStr'"
$run "${cmdLineArr[@]}"
# return code, see trap EXIT above
commandLineReturnCode=$?
# self explain
# TODO create a "warning' failed file for this ?
(( maxWarn > 0 && $SECONDS > maxWarn )) && _log "Warning: script has been running more that ${maxWarn}s"
(( minWarn > 0 && $SECONDS < minWarn )) && _log "Warning: script has been running less that ${minWarn}s"
if (( commandLineReturnCode == 0 ))
then
(( deleteFailedFileOnZero )) && $run _remove_file $failedFile
else
(( createFailedFileIfNotZero )) && _create_file "$failedFile" "$cmdLineStr: failed with exit code: $commandLineReturnCode"
fi
# one last for the road
_log "stopping: '$cmdLineStr' (exitCode: $commandLineReturnCode, duration: ~${SECONDS}s)"