Gentoo Archives: gentoo-commits

From: "Mike Frysinger (vapier)" <vapier@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] gentoo-x86 commit in sys-process/cronbase/files: run-crons-0.3.6
Date: Thu, 23 Jul 2015 02:57:46
Message-Id: 20150723025741.9DF80BD@oystercatcher.gentoo.org
1 vapier 15/07/23 02:57:41
2
3 Added: run-crons-0.3.6
4 Log:
5 Rewrite run-crons in POSIX shell #530416 by Alexander Hof.
6
7 (Portage version: 2.2.20/cvs/Linux x86_64, signed Manifest commit with key D2E96200)
8
9 Revision Changes Path
10 1.1 sys-process/cronbase/files/run-crons-0.3.6
11
12 file : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/sys-process/cronbase/files/run-crons-0.3.6?rev=1.1&view=markup
13 plain: http://sources.gentoo.org/viewvc.cgi/gentoo-x86/sys-process/cronbase/files/run-crons-0.3.6?rev=1.1&content-type=text/plain
14
15 Index: run-crons-0.3.6
16 ===================================================================
17 #!/bin/sh
18 #
19 # $Header: /var/cvsroot/gentoo-x86/sys-process/cronbase/files/run-crons-0.3.6,v 1.1 2015/07/23 02:57:41 vapier Exp $
20 #
21 # 12 Oct 2008; Thilo Bangert <bangert@g.o> run-crons:
22 # ignore emacs backup files (bug #237200)
23 # include logging patch (bug #140869)
24 #
25 # 08 Mar 2005; Aaron Walker <ka0ttic@g.o> run-crons:
26 # Ignore the error messages from find caused by race conditions, since
27 # we could care less about the error as long as the file has been removed.
28 # See bug 8506.
29 #
30 # 06 May 2004; Aron Griffis <agriffis@g.o> run-crons:
31 # Make the locking actually work. The old code was racy.
32 # Thanks to Mathias Gumz in bug 45155 for some cleanups.
33 #
34 # 23 Jun 2002; Jon Nelson <jnelson@g.o> run-crons:
35 # fixed a race condition, where cron jobs and run-crons wanted to
36 # delete touch files
37 #
38 # 20 Apr 2002; Thilo Bangert <bangert@g.o> run-crons:
39 # moved lastrun directory to /var/spool/cron/lastrun
40 #
41 # Author: Achim Gottinger <achim@g.o>
42 #
43 # Mostly copied from SuSE
44 #
45 # this script looks into /etc/cron.[hourly|daily|weekly|monthly]
46 # for scripts to be executed. The info about last run is stored in
47 # /var/spool/cron/lastrun
48
49 LOCKDIR=/var/spool/cron/lastrun
50 LOCKFILE=${LOCKDIR}/lock
51
52 # Usage: log <level> <args to logger>
53 # Log a message via syslog.
54 log() {
55 local level="$1"
56 shift
57 logger -i -p "cron.${level}" -t run-crons "$@"
58 }
59
60 mkdir -p ${LOCKDIR}
61
62 # Make sure we're not running multiple instances at once.
63 # Try twice to lock, otherwise give up.
64 i=0
65 while [ $(( i += 1 )) -le 2 ] ; do
66 ln -sn $$ ${LOCKFILE} 2>/dev/null && break
67
68 # lock failed, check for a running process.
69 # handle both old- and new-style locking.
70 cronpid=$(readlink ${LOCKFILE} 2>/dev/null) ||
71 cronpid=$(cat ${LOCKFILE} 2>/dev/null) ||
72 continue # lockfile disappeared? try again
73
74 # better than kill -0 because we can verify that it's really
75 # another run-crons process
76 cmdline1=$(cat /proc/${cronpid}/cmdline 2>/dev/null) || :
77 cmdline2=$(cat /proc/$$/cmdline)
78 if [ "${cmdline1}" = "${cmdline2}" ] ; then
79 # whoa, another process is really running
80 exit 0
81 else
82 rm -f ${LOCKFILE}
83 fi
84 done
85
86 # Check to make sure locking was successful
87 if [ ! -L "${LOCKFILE}" ] ; then
88 echo "Can't create or read existing ${LOCKFILE}, giving up"
89 exit 1
90 fi
91
92 # Set a trap to remove the lockfile when we're finished
93 trap "rm -f ${LOCKFILE}" EXIT HUP INT QUIT TERM
94
95
96 EXIT_STATUS=0
97 for BASE in hourly daily weekly monthly ; do
98 CRONDIR=/etc/cron.${BASE}
99
100 test -d $CRONDIR || continue
101
102 if [ -e ${LOCKDIR}/cron.$BASE ] ; then
103 case $BASE in
104 hourly)
105 #>= 1 hour, 5 min -=> +65 min
106 TIME="-cmin +65" ;;
107 daily)
108 #>= 1 day, 5 min -=> +1445 min
109 TIME="-cmin +1445" ;;
110 weekly)
111 #>= 1 week, 5 min -=> +10085 min
112 TIME="-cmin +10085" ;;
113 monthly)
114 #>= 31 days, 5 min -=> +44645 min
115 TIME="-cmin +44645" ;;
116 esac
117
118 find ${LOCKDIR} -name cron.$BASE $TIME -exec rm {} \; 2>/dev/null || :
119 fi
120
121 # if there is no touch file, make one then run the scripts
122 if [ ! -e ${LOCKDIR}/cron.$BASE ] ; then
123 touch ${LOCKDIR}/cron.$BASE
124
125 set +e
126 for SCRIPT in $CRONDIR/* ; do
127 if [ -x "${SCRIPT}" ] && [ ! -d "${SCRIPT}" ] ; then
128 # Filter out files people do not expect to be executed.
129 case ${SCRIPT} in
130 .*|*~) continue ;;
131 esac
132
133 log info "($(whoami)) CMD (${SCRIPT})"
134 $SCRIPT
135 ret=$?
136 if [ ${ret} -ne 0 ] ; then
137 log err "CMD (${SCRIPT}) failed with exit status ${ret}"
138 EXIT_STATUS=1
139 fi
140 fi
141 done
142 fi
143 done
144
145 # Clean out bogus cron.$BASE files with future times
146 touch ${LOCKDIR}
147 find ${LOCKDIR} -newer ${LOCKDIR} -exec /bin/rm -f {} \; 2>/dev/null || :
148
149 exit ${EXIT_STATUS}