1

I'm trying to learn a little about cron.

I edited crontab -e to have this line:

 */10 * * * * /usr/bin/touch /home/dkb/Desktop/test.txt

It works and I see test.txt changing its "Date Modified" value every ten min in Thunar and there are corresponding entries in /var/log/syslog:

Mar 13 10:50:01 dkb-lappy CRON[3978]: (dkb) CMD (/usr/bin/touch /home/dkb/Desktop/test.txt)
Mar 13 11:00:01 dkb-lappy CRON[4066]: (dkb) CMD (/usr/bin/touch /home/dkb/Desktop/test.txt)
Mar 13 11:10:01 dkb-lappy CRON[4099]: (dkb) CMD (/usr/bin/touch /home/dkb/Desktop/test.txt)

But, if I use

*/10 * * * * /usr/bin/touch /home/dkb/Desktop/$(date +%H:%M:%S).txt  

no file is created at all in ~/Desktop and syslog has this:

Mar 13 11:30:01 dkb-lappy CRON[4241]: (dkb) CMD (/usr/bin/touch /home/dkb/Desktop/$(date +)
Mar 13 11:30:01 dkb-lappy CRON[4240]: (CRON) info (No MTA installed, discarding output)
Mar 13 11:40:01 dkb-lappy CRON[4251]: (dkb) CMD (/usr/bin/touch /home/dkb/Desktop/$(date +)
Mar 13 11:40:01 dkb-lappy CRON[4250]: (CRON) info (No MTA installed, discarding output)

I checked that /usr/bin/touch /home/dkb/Desktop/$(date +%H:%M:%S).txt works properly in the terminal.

So what am I doing wrong?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
DK Bose
  • 947

1 Answers1

4

crontab treats % specially. You need to escape it with a backslash.

From man 5 crontab:

Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

Thus, to get date to work properly:

*/10 * * * * /usr/bin/touch /home/dkb/Desktop/$(date +\%H:\%M:\%S).txt  
John1024
  • 74,655