Justin Egbert
Justin Egbert

Reputation: 17

cron task not writing to file

I'm trying to append the current date and time to a log file every minute using cron. I want the date and time to be formatted in a specific way.

This works:

* * * * * date >> /home/user/time1.txt

This doesn't:

* * * * * date +%Y%m%d%H%M%S >> /home/user/time2.txt

Any insight is much appreciated!

Upvotes: 0

Views: 75

Answers (2)

Puneet Tripathi
Puneet Tripathi

Reputation: 422

escape % with \ and then it should work.

Upvotes: 0

Inian
Inian

Reputation: 85590

The problem is that cron treats % as newlines. You need to escape them

From crontab POSIX man page:

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.

* * * * * date +\%Y\%m\%d\%H\%M\%S >> /home/user/time2.txt

Upvotes: 2

Related Questions