Bug 498303 - anacron causes cron.daily to run twice if it is scheduled late in the day
Summary: anacron causes cron.daily to run twice if it is scheduled late in the day
Keywords:
Status: CLOSED CURRENTRELEASE
Alias: None
Product: Fedora
Classification: Fedora
Component: anacron
Version: 10
Hardware: i686
OS: Linux
low
medium
Target Milestone: ---
Assignee: Marcela Mašláňová
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2009-04-29 19:42 UTC by Oliver Henshaw
Modified: 2009-06-18 12:21 UTC (History)
2 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2009-06-18 12:21:03 UTC
Type: ---
Embargoed:


Attachments (Terms of Use)

Description Oliver Henshaw 2009-04-29 19:42:54 UTC
Description of problem:

I have a desktop system that is off at night. I changed crontab to run cron.daily at 6pm, so that it performs daily tasks and backups at a predictable time. Unfortunately, anacron only has a granularity in days, so now runs cron.daily when it is switched on earlier in the day. So now daily jobs are run twice..

This will be a problem for any desktop or laptop, if the owner wants to run cron.* jobs late in the day. This is mitigated in the default case that cron.daily/weekly/monthly runs at 4am, but not eliminated.

The best algorithm that comes to mind is that: anacron acts normally if the anacron timestamp is older than (period+1) days. Else if the timestamp is period days ago, anacron should only run if the time is after the scheduled hour in the relevant crontab.

I don't know whether that would cause problems for other common use cases, or what subtleties are involved in parsing the hour field out of crontab. But then I don't make any claims to being a domain expert.

An alternative would be to teach anacron to write/read a new kind of timestamp that includes the hour of the day.

A workaround would be to increase the periods in the default anacrontab by one day, and live with the occasional missed (delayed) anacron run.


Version-Release number of selected component (if applicable):
cronie-1.2-7.fc10.i386
crontabs-1.10-26.fc10.noarch
anacron-2.3-66.fc10.i386


Steps to Reproduce:
1. Set e.g. "02 18 * * * root run-parts /etc/cron.daily" in /etc/crontab
2. Power on a few hours before before 6pm, power off a few hours after.
3. Observe /var/log/cron the next day.
  
Actual results:
cron.daily run twice,

Expected results:
cron.daily run once.

Note: Obviously, this would affect cron.weekly and cron.monthly too, if they are scheduled later in the day they are due to run. This may be unduly expensive if these tasks are more costly than the daily tasks.

Comment 1 Donald Cohen 2009-04-30 03:36:14 UTC
I see anacron running everything in cron.daily AFTER cron does so.
I never turn off my system.  Cron runs things at about 6:50 AM and then anacron runs them again around 7:50.
I don't understand at this point who/what is running anacron.
I've added tracing to /etc/cron.daily/0anacron, which now says (other than comment lines):

test -x /usr/sbin/anacron || exit 0
anacron -u cron.daily

# Tuesday 2009/04/28 debugging
echo ==== /etc/cron.daily/0anacron >> /tmp/whoruns
date >> /tmp/whoruns
cat /var/spool/anacron/cron.daily >> /tmp/whoruns

The output in /tmp/whoruns:
==== /etc/cron.daily/0anacron
Wed Apr 29 06:54:45 PDT 2009
20090429
==== /etc/cron.daily/0anacron
Wed Apr 29 07:52:29 PDT 2009
20090429

So I don't understand why anacron doesn't stop when it reads the timestamp written by cron.

Comment 2 Donald Cohen 2009-04-30 03:43:29 UTC
One more detail - the way I know it was cron that ran first is that some of the cron jobs send me mail, and the one with sender and subject indicating cron comes an hour before the one with sender and subject indicating anacron.

Comment 3 Oliver Henshaw 2009-04-30 08:59:24 UTC
Donald: As I said, I'm not a cron expert. So take this with a pinch of salt, and maybe go to a support channel if you want help tracking down the cause of your problem.

But while the symptoms are clearly different, maybe the underlying cause is really the same?

Have you checked /var/log/cron to see when anacron runs things, and which runs first? This might be an interaction between the anacron delay in /etc/anacrontab and the cron delay in /etc/cron.daily/000delay.

Comment 4 Marcela Mašláňová 2009-04-30 10:41:46 UTC
to Oliver: Are you saying that it runs at 6pm and then after midnight or is it running twice before midnight?

to Donald: The last update crontabs-1.10-26.fc10.noarch and anacron-2.3-66.fc10.i386 should fix behaviour, which you are speaking of. I was assured by users that it "seems" to be fix. Could you tell me if you have updated versions? The problems could be also done by delay of anacron.

The way how it was written didn't count with different user cases. Next version of anacron in F-11 was partly rewritten. There is new option, which allows user to set the time period, when the job should be started. I can't backport this option back to F-10 because I've changed the cooperation of anacron, cron and crontabs since F-11.

Comment 5 Donald Cohen 2009-04-30 16:42:44 UTC
(In reply to comment #3)
> Have you checked /var/log/cron to see when anacron runs things, and which runs
> first? This might be an interaction between the anacron delay in
> /etc/anacrontab and the cron delay in /etc/cron.daily/000delay.  

Apr 30 06:02:01 number11 CROND[7307]: (root) CMD (run-parts /etc/cron.daily)
 ...
Apr 30 06:54:45 number11 run-parts(/etc/cron.daily)[7307]: starting 0anacron
Apr 30 06:54:45 number11 anacron[8023]: Updated timestamp for job `cron.daily' to 2009-04-30
 ...
Apr 30 06:59:45 number11 anacron[7196]: Job `cron.daily' started
Apr 30 06:59:45 number11 run-parts(/etc/cron.daily)[10406]: starting 000-delay.cron
 ...
Apr 30 07:52:29 number11 run-parts(/etc/cron.daily)[10406]: starting 0anacron
Apr 30 07:52:29 number11 anacron[11138]: Updated timestamp for job `cron.daily' to 2009-04-30

Comment 6 Donald Cohen 2009-04-30 16:48:48 UTC
(In reply to comment #4)
> to Donald: The last update crontabs-1.10-26.fc10.noarch and
> anacron-2.3-66.fc10.i386 should fix behaviour, which you are speaking of. I was
> assured by users that it "seems" to be fix. Could you tell me if you have
> updated versions? The problems could be also done by delay of anacron.
from yum.log:
Mar 01 01:48:35 Updated: crontabs-1.10-26.fc10.noarch

Comment 7 Oliver Henshaw 2009-04-30 21:34:49 UTC
It's run twice a day (once just after switch-on and once as scheduled at/after 6pm), e.g:

Apr 27 09:50:39 mostin crond[2039]: (CRON) STARTUP (1.2)
Apr 27 09:50:40 mostin crond[2039]: (CRON) INFO (running with inotify support)
Apr 27 09:50:43 mostin anacron[2088]: Anacron 2.3 started on 2009-04-27
Apr 27 09:50:43 mostin anacron[2088]: Will run job `cron.daily' in 65 min.
Apr 27 09:50:43 mostin anacron[2088]: Jobs will be executed sequentially
Apr 27 10:55:43 mostin anacron[2088]: Job `cron.daily' started
Apr 27 10:55:51 mostin run-parts(/etc/cron.daily)[4031]: starting 000-delay.cron
Apr 27 12:00:15 mostin run-parts(/etc/cron.daily)[4921]: finished 000-delay.cron
Apr 27 12:00:16 mostin run-parts(/etc/cron.daily)[4031]: starting 0anacron
Apr 27 12:00:17 mostin run-parts(/etc/cron.daily)[4930]: finished 0anacron
Apr 27 12:00:17 mostin anacron[4928]: Updated timestamp for job `cron.daily' to 2009-04-27
...
Apr 27 12:31:41 mostin anacron[2088]: Job `cron.daily' terminated (mailing output)
Apr 27 18:02:01 mostin run-parts(/etc/cron.daily)[10854]: starting 000-delay.cron
Apr 27 19:01:02 mostin CROND[18443]: (root) CMD (run-parts /etc/cron.hourly)
Apr 27 19:06:19 mostin run-parts(/etc/cron.daily)[19566]: finished 000-delay.cron
Apr 27 19:06:19 mostin run-parts(/etc/cron.daily)[10854]: starting 0anacron
Apr 27 19:06:19 mostin anacron[19580]: Updated timestamp for job `cron.daily' to 2009-04-27
Apr 27 19:06:19 mostin run-parts(/etc/cron.daily)[19584]: finished 0anacron


Probably /etc/cron.d/anacron should be run sufficiently after cron.daily that it doesn't overlap with 000delay. This doesn't help with anacron running after startup, though. Would it be too insane for /etc/init.d/anacron to parse /etc/cron.d/anacron? Or would is it better left to upstart to be that smart?

If F11 allows to constrain anacron and cron to run within a certain time window, then probably a lot of this no longer matters. I'll check out the preview to see how it behaves.

Comment 8 Marcela Mašláňová 2009-05-11 10:22:41 UTC
The problem is cron runs all jobs in cron.daily and don´t check anacron lock at all. There were different hacks how to solve it, but none of them work for all use-cases. If you don´t switch off your computer, then simply remove anacron. For other use cases I´ll be looking for new hack.

Comment 9 Donald Cohen 2009-05-11 16:35:47 UTC
(In reply to comment #8)
> The problem is cron runs all jobs in cron.daily and don´t check anacron lock at
> all. There were different hacks how to solve it, but none of them work for all
> use-cases. If you don´t switch off your computer, then simply remove anacron.
> For other use cases I´ll be looking for new hack.  

I don't think this is my problem.  Anacron runs ~1 hour after crond (on the same day), and it therefore ought to see the up-to-date timestamp and do nothing.
Can you tell me what causes anacron to run?

Comment 10 Marcela Mašláňová 2009-05-12 07:24:34 UTC
Cron runs every jobs in directory and it runs at time in /etc/crontab + delay which is generated for every computer.
Anacron is started after  start of computer with daemon with some delay, which is defined in /etc/anacrontab + delay. Then run at 5:02 from /etc/cron.d/anacron, where is checked whether daily run. This time should be changed according to change in /etc/crontab.

Comment 11 Donald Cohen 2009-05-12 18:14:49 UTC
(In reply to comment #10)
> Cron runs every jobs in directory and it runs at time in /etc/crontab + delay
> which is generated for every computer.
> Anacron is started after  start of computer with daemon with some delay, which
> is defined in /etc/anacrontab + delay. Then run at 5:02 from
> /etc/cron.d/anacron, where is checked whether daily run. This time should be
> changed according to change in /etc/crontab.  

Thank you.  This was my problem.  I had changed my cron.daily time to 6:15.
Now that I have changed my anacron time to later than that I'm in good shape.
This seems to be a recent change - I don't see anacrontab in older Fedora versions.

Comment 12 Marcela Mašláňová 2009-05-13 11:27:34 UTC
anacrontab is there ages. The problem is the job which run at 5:02. I could mention it in man page.

Comment 13 Donald Cohen 2009-06-10 20:25:15 UTC
I've just moved to fc11 and now the problem resurfaces.
I've changed anacrontab to cause it to run after cron.daily.
What else has changed about the way things work?

Comment 14 Marcela Mašláňová 2009-06-11 07:57:03 UTC
In F-11 are jobs running only from /etc/anacrontab. The /etc/crontab should be empty. So there won't be needed any workarounds anymore because jobs will be running only once.

Comment 15 Donald Cohen 2009-06-11 16:00:33 UTC
(In reply to comment #14)
> In F-11 are jobs running only from /etc/anacrontab. The /etc/crontab should be
> empty. So there won't be needed any workarounds anymore because jobs will be
> running only once.  

I see, my problem is that I still use the old crontab.
Man crontab doesn't mention that crontab should now be empty.
Should it at least contain the stuff like mailto ?
I notice that anacrontab has entries only for daily/weekly/monthly.
How does hourly get done?

Comment 16 Marcela Mašláňová 2009-06-16 12:18:38 UTC
> I see, my problem is that I still use the old crontab.
> Man crontab doesn't mention that crontab should now be empty.
> Should it at least contain the stuff like mailto ?
I will comment this in crontab more. The stuff should stay in case you'll use this crontab for some jobs.
> I notice that anacrontab has entries only for daily/weekly/monthly.
> How does hourly get done?  
Hourly are executed by cron job in /etc/cron.d/.

Comment 17 Donald Cohen 2009-06-16 19:45:42 UTC
(In reply to comment #16)
> > I notice that anacrontab has entries only for daily/weekly/monthly.
> > How does hourly get done?  
> Hourly are executed by cron job in /etc/cron.d/.  

But crontab contains NO entries.
I guess you're telling me that it runs whatever is in /etc/cron.d ...
This is not all that clear from the man pages.

Comment 18 Marcela Mašláňová 2009-06-18 12:21:03 UTC
The man page with details is now in testing repo, it will be under man 4 crontabs. The setting of anacron is mentioned in anacron(tab) manual.

Documented in: crontabs-1.10-30.fc11


Note You need to log in before you can comment on or make changes to this bug.