-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.sh
executable file
·66 lines (56 loc) · 1.06 KB
/
cron.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
#!/bin/bash -m
if [ "x$1" == "x1" ]
then
rm -f /tmp/*.cronwget.pid
fi
TODO="echo 123 | wall"
TIMETOSLEEP=10
SELFNAME=$0
OLDPIDFILE=`ls -1 /tmp/*.cronwget.pid 2>/dev/null | head 2>/dev/null`
PID=`echo $OLDPIDFILE | xargs basename 2>/dev/null | awk -F "." '{print $1}'`
NEWPIDFILE="/tmp/$$.cronwget.pid"
#search for md5 program
which md5sum 2>/dev/null 1>&2
RV=$?
[ $RV -eq 0 ] && {
MD5="md5sum"
}
[ $RV -ne 0 ] && {
which md5 2>/dev/null 1>&2
RV=$?
[ $RV -eq 0 ] && {
MD5="md5"
}
[ $RV -ne 0 ] && {
echo "cannot find md5 program"
exit 1
}
}
function runcron {
$MD5 $SELFNAME > $NEWPIDFILE
while [ true ] ; do
sleep $TIMETOSLEEP &
sh -c "$TODO"
wait
if [ "x`$MD5 $SELFNAME`" != "x`cat $NEWPIDFILE`" ]
then
#file was midifed
break
fi
done
nohup $SELFNAME 1 2>/dev/null 1>&2 &
}
#check for run cron
if [ ! -f "$OLDPIDFILE" ]
then
#no pid file found.
runcron
exit
fi
#check for currently run
ps ax | awk '{ print $1 }' | grep -q $PID || {
#pid file is not valid
rm -f /tmp/*.cronwget.pid
runcron
}
#nothing to be done — process run