4

Recently I wrote a simple little script to backup a directory to S3. My goal is to run daily, weekly and monthly backups the have different retention rules based on the frequency in S3.

Everything is working pretty well with one exception. My WEEKLY cron job runs every day. Here's the crontab:

0 3 1 * * /usr/s3_sync_dir /dropbox MONTHLY
0 2 2-31 * THU /usr/s3_sync_dir /dropbox WEEKLY
0 3 2-31 * * /usr/s3_sync_dir /dropbox DAILY

So the MONTHLY job runs at 3am on the 1st of every month. The WEEKLY should then run on Thursdays at 2am if it's not the 1st. And the DAILY just runs every day that's not the 1st. This online validator confirms that the WEEKLY should just run every Thursday.

Anyone have thoughts as to why my WEEKLY job runs every morning?

This is on Ubuntu 16.04.2 LTS.

  • 3
    Check if you have /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly directories. It's simpler to just put things there I guess ? – ychaouche Apr 27 '17 at 13:35
  • also, your weekly line specfies to run every day of the month from 2nd to 31st day, excluding the first day. – ychaouche Apr 27 '17 at 13:37
  • Have you tried using 4 instead of THU? – ceejayoz Apr 27 '17 at 13:38
  • @ychaouche No, the THU in the "day of week" fifth column should restrict it to Thursdays. I don't know how recent that syntax (versus the 0-6 values) is though. – ceejayoz Apr 27 '17 at 13:39
  • Here's how weekly jobs are run from /etc/crontab 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ). That means you probably should replace 2-31 with * – ychaouche Apr 27 '17 at 13:39
5

From the manual (man 5 crontab)

Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example, ``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of
each month, plus every Friday. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).

I guess you should turn your 2-31 into *

OR

you can use @daily, @weekly and @monthly.

OR

you can link to your scripts in /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly.

  • Ended up going with the /etc/cron.* directories. And instead of of trying to exclude them from each other I just adjusted the time of day they'd execute in the /etc/crontab file. Worst case scenario is I'm backing up data too often. – Staros Apr 27 '17 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.