How To Use The date Command With cron

Tim Goodman
2 min readDec 8, 2023
Photo by Vicko Mozara on Unsplash

As you probably already know cron is a scheduler in *nix type systems and that the date command can be used with the --date parameter and formatting specifiers such as %d to display the day of the month (01 to 31). There are a lot of articles on how to use the cron and date commands so this article won’t go into great detail on how to use them. Instead the purpose of this article is to discuss the caveats of using the date command with formatting specifiers in your crontab.

Let’s say you want to run a command and send the output to a different log file every day. A simple example is:

echo “test” &>> /home/tgoodman/date_test_$(date ‘+%m%d%Y’).log

If I run this from the command line it successfully runs and appends “test” to the file named “date_test_12072023.log”. If the log file did not already exist it would have created it first.

The issue is if I add this same statement to my crontab to run every minute:

* * * * * echo “test” &>> /home/tgoodman/date_test_$(date ‘+%m%d%Y’).log

Nothing gets added to the log file and it won’t be created if it didn’t already exist. Checking the /var/log/cron log file I see the entry:

Dec 7 13:30:01 TimWSL-Alma8 CROND[479]: (tgoodman) CMD (echo “test” &>> /home/tgoodman/date_test_$(date ‘+)

--

--