1

I am trying to get a cronjob in /etc/cron.d/ to run the first Saturday of every month.

Here is what I have so far:

0 1 1-7 * 6 [`date +\%d` == 06] root && /home/test/cron-test.sh

I change the system date Jan 14, 2017 (a Saturday in 2017) at midnight and checking tail /var/log/syslog | grep cron-test reports:

Jan 14 00:59:21 Inspiron-1545 cron[936]: Error: bad username; while reading /etc/cron.d/cron-check1
Jan 14 00:59:21 Inspiron-1545 cron[936]: (*system*cron-check1) ERROR (Syntax error, this crontab file will be ignored)

What am I doing wrong?

4

System-wide crontabs have the five date and time fields on each line, then the username to run the cronjob as, then the command line. In yours, the time fields seem ok, but the sixth field [`date is rather odd for a username. The error message hints at that.

Since you have root mentioned after the snippet in brackets, I think you may have just misplaced the username, so you should have:

0 1 1-7 * 6 root [`date +\%d` == 06] && /home/test/cron-test.sh

This still has the issue that [ is a command just like others, so you have to use whitespace after the [ and before the ]. And == isn't standard for comparison, = is. Also, for command substitution, $(cmd) is just nicer than backticks, so let's try:

0 1 1-7 * 6 root [ $(date +\%d) = 06 ] && /home/test/cron-test.sh

But date +%d tells the day of month, and now you're only running on the sixth day of the month (regardless of weekday). The third and fifth fields (day of month and day of week) work a a bit oddly together: the cronjob runs if either of them matches. But we want to run only on a Saturday, and only on the first to seventh days of month, so we have to check one of the conditions manually.

0 1 1-7 * * root [ $(date +\%u) -eq 6 ] && /home/test/cron-test.sh

The cronjob itself runs on all of the first seven days of the month, but the manual test [ $(date +%u) -eq 6 ] checks that the day of week is Saturday, before actually running the main command.

(Some versions of the crontab(5) manual page have an example almost like that, but I can't find an online version of it.)

  • Logic says you don't need the date test with just 0 1 1-7 * 6 root /home/test/cron-test.sh. 01:00am, the first 7 days of a month, and on Saturday only. That's only going to execute once each month. There is only going to be one Saturday at some point in the first 7 days of any month. – Deathgrip Jun 16 '17 at 21:57
  • 1
    @Deathgrip, logic yes, crontab no. The man page explicitly states that if both are restricted, the command runs if either field matches: ""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." – ilkkachu Jun 16 '17 at 22:00
  • @ilkkachu - Thanks for your detailed and excellent solution!!! It works perfectly!!!! – john7a8 Jun 17 '17 at 15:53
  • @Deathgrip - Thanks for your post also. BTW - I did try the solution you recommended, the problem I ran into is that it ran every Saturday of the month. – john7a8 Jun 17 '17 at 15:53

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.