Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

cron - date +"%Y-%m-%d" in crontab

If I put the following in crontab -e:

* * * * * date +"%Y-%m-%d" > /home/apps/temp/env.txt

there is no env.txt created.

If I change the above line to:

* * * * * date > /home/apps/temp/env.txt

env.txt is created properly.

How can I format date in cron?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to escape each one of the %:

* * * * * date +"\%Y-\%m-\%d" > /home/apps/temp/env.txt

Or even better, remove the quotes and leave like this:

* * * * * date +\%Y-\%m-\%d > /home/apps/temp/env.txt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...