0

How does anacron determine daily, weekly and monthly job to run?

  1. Which time of the previous job will anacron use to decide whether to run the job again: start time or finish time of the previous job? Is it correct that anacron uses only the finish time and cron only concerns the start time?
  2. Does daily mean exactly 24 hours, or just different calendar days will allow a daily job to run?

    Does weekly mean exactly 7 days, or just different calendar weeks will allow a weekly job to run?

    Does monthly mean exactly 30 days, or just different calendar months will allow a monthly job to run?

    The timestamp files are only accurate up to day, so I guess the answer to the daily question is a different "calender" day not 24 hours?

    $ sudo cat /var/spool/anacron/cron.daily 
    20181101
    $ sudo cat /var/spool/anacron/cron.weekly 
    20181028
    $ sudo cat /var/spool/anacron/cron.monthly 
    20181011
    

Some example. Suppose a daily job starts to run at 4am today, and anacron will run only once tomorrow at 6am.

  • If the job finishes running at 11am today, Which time will anacron use to decide whether to run the job: start time or finish time?

  • If the job finishes running at 5am today, will anacron decides to run it then?

  • If the job finishes running at 3am tommorrow, will anacron decides to run it then?

Thanks.

Tim
  • 101,790

1 Answers1

4

anacron runs a job if it hasn’t been run in its defined period. Thus a job which is defined as running every 7 days will be run if it hasn’t run in the last 7 days. anacron calculates this only by looking at calendar days: it loads the timestamp of the last run, calculates the current “day number”, and subtracts the two.

  1. anacron updates timestamps after jobs execute, so it uses the finish date as the start of the period during which the corresponding job shouldn’t be run again. cron only compares the current date and time with a given start specifier, so yes, it only considers start times.

  2. Daily means that the last time a job was run must be yesterday or earlier. Weekly means that the last time a job was run must be 7 days ago or earlier. Monthly is a bit different; it means that the last time a job was run must be n days ago or earlier, where n is the number of days in the previous month, with a bypass when the number of elapsed days is exactly equal to the number of days in the current month.

    Given your examples:

    • a daily job finishing at 11am or 5am today will run again tomorrow;
    • a daily job finishing at 3am tomorrow will run again the day after tomorrow.
Stephen Kitt
  • 434,908
  • Thanks. I was wondering what you meant by " with a bypass when the number of elapsed days is exactly equal to the number of days in the current month"? – Tim Nov 02 '18 at 22:43
  • When is which that decides: the number of days in the previous month and the number of days in the current month? – Tim Nov 02 '18 at 22:50