cron 已添加但未执行当前时间

cron is added but not executing current time

following is my recipe I am trying to create a new text file with current time and date

cron 'cron_disply_time' do
  minute '*/1'
  command "echo #{Time.new.strftime("%Y-%m-%d %H:%M")} > /home/vagrant/learn-cron/cookbooks/t.txt"
  action :create
end

Its always taking the time when the job was added

*/1 * * * * echo 2020-05-07 14:06 > /home/vagrant/learn-cron/cookbooks/t.txt

can some please let me know how can I make it executable with the recipe only

cron 资源生成一个静态文件,其中包含命令,所以是的,它会将 1 和相同的字符串放入文件中。

在bash当前时间可以调用date

$ date
Fri May  8 09:58:39 EEST 2020

因此您可以在 cron 中使用此函数来获取当前时间,如下所示:

cron 'cron_disply_time' do
  minute '*'
  command 'date "+%F %H:%M" > /home/vagrant/learn-cron/cookbooks/t.txt'
  action :create
end
cron 'cron_disply_current_time' do
  minute '*/1'
  command 'NOW=$(date +"%T") && sudo echo "Current time:"  $NOW > /home/vagrant/current_time.txt'
  action :create
end

date will show the current date

NOW=$(date +"%T") will show the time

sudo echo "Current time:" $NOW > /home/vagrant/current_time.txt' will add "Current time:" 11:14:19 into current_time.txt file