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.shI 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-check1Jan 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?
/etc/cron.d/cronjob
cron
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.shThis 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.shBut 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.shThe 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.)