summaryrefslogtreecommitdiff
blob: 7f0ad86c7fc68b52b7747dfb5e03358a2cdde373 (plain)
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
#!/bin/sh
#
# $Header: /var/cvsroot/gentoo-x86/sys-process/cronbase/files/run-crons-0.2.1,v 1.1 2005/03/04 23:24:54 ciaranm Exp $
#
# 23 Jun 2002; Jon Nelson <jnelson@gentoo.org> run-crons:
#
# fixed a race condition, where cron jobs and run-crons wanted to delete
# touch files
#
# 20 Apr 2002; Thilo Bangert <bangert@gentoo.org> run-crons:
#
# moved lastrun directory to /var/spool/cron/lastrun
#
# Author: Achim Gottinger <achim@gentoo.org>
#
# Mostly copied from SuSE
#
# this script looks into /etc/cron.[hourly|daily|weekly|monthly]
# for scripts to be executed. The info about last run is stored in 
# /var/spool/cron/lastrun

mkdir -p /var/spool/cron/lastrun

# Make sure its not running multiple instances at once.
if test -f /var/spool/cron/lastrun/lock
then
    cronpid=`cat /var/spool/cron/lastrun/lock`
    if `kill -0 $cronpid >/dev/null 2>&1`
    then
	exit 0
    fi
fi

echo "$$" >/var/spool/cron/lastrun/lock

for BASE in hourly daily weekly monthly
do
    CRONDIR=/etc/cron.${BASE}

    test -d $CRONDIR || continue

    if test -e /var/spool/cron/lastrun/cron.$BASE
    then
	case $BASE in
	    hourly)
		#>= 1 hour, 5 min -=> +65 min
		TIME="-cmin +65" ;;
	    daily)
		#>= 1 day, 5 min -=> +1445 min
		TIME="-cmin +1445"  ;;
	    weekly)
		#>= 1 week, 5 min -=> +10085 min
		TIME="-cmin +10085"  ;;
	    monthly)
		#>= 31 days, 5 min -=> +44645 min
		TIME="-cmin +44645" ;;
	esac
	find /var/spool/cron/lastrun -name cron.$BASE $TIME -exec rm {} \;
    fi

    # if there is no touch file, make one then run the scripts 
    if test ! -f /var/spool/cron/lastrun/cron.$BASE
    then

	touch /var/spool/cron/lastrun/cron.$BASE

	set +e
	for SCRIPT in $CRONDIR/*
	do
	    test -d $SCRIPT && continue
	    if test -x $SCRIPT ; then
		$SCRIPT
	    fi
	done
    fi
done

# Remove lock, we're done.
rm -f /var/spool/cron/lastrun/lock

touch /var/spool/cron/lastrun
find /var/spool/cron/lastrun -newer /var/spool/cron/lastrun -exec /bin/rm -f {} \;