-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathds-common
More file actions
executable file
·42 lines (38 loc) · 1.11 KB
/
ds-common
File metadata and controls
executable file
·42 lines (38 loc) · 1.11 KB
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
#!/bin/sh
log() { echo ${prog} $(date +%F_%T) "$@" >> /home/user/xxx; }
fail() { err=$1; log Error "${@}"; exit $err; }
all2log() { exec >> /home/user/xxx 2>&1; }
prog=$(basename $0)
progdir=$(cd `dirname $0`;pwd); d=$progdir
pid=$$
[ "$prog" = "ds-common" ] && { echo Do not run ds-common directly; exit 99; }
[ $(id -u) -eq 0 ] || exec sudo ${progdir}/$prog "$@"
log Started $@
while [ $@ ]; do
if ([ "$1" = "LOOP" ]); then
loop_mode=true; log Will go into loop_mode
fi
type -t args_core && args_core
shift
done
loop_delay=60
loop() {
[ $loop_mode ] || { nohup "$0" LOOP& exit 0; }
get_lock $2
log going loopy pid=${pid}
while :; do
test_lock $LOCKFILE
loop_core
gap=$loop_delay
sleep $gap
done
}
get_lock() {
[ "x$LOCKFILE" = "x" ] && LOCKFILE=${1:-/tmp/$prog.lock}
[ -f "$LOCKFILE" ] && { log Lockfile already exists - exiting; exit 0; }
echo $progdir/$prog $$ > $LOCKFILE; sleep 1
grep -q " ${pid}$" $LOCKFILE || { log Someone else got the lock before me; exit 0; }
}
test_lock() {
grep -q " ${pid}$" $LOCKFILE 2>/dev/null || { log My log has vanished or been stollen; exit 0; }
}