Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I'm trying to learn a little about cron and would appreciate some help.

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?

share|improve this question

marked as duplicate by Anthon, terdon, jasonwryan, Zelda, slm Mar 13 at 11:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

up vote 3 down vote accepted

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  
share|improve this answer
    
Worked perfectly. Thanks! –  DK Bose Mar 13 at 6:45

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