2017年2月16日 星期四

[linux] crontab using date as variable in command / crontab 中使用 date 當參數


有個需求是定期的 export ga 資料到 Amazon s3 上,在使用 Amazon EMR 做分析。

# export ga
0 0 * * * /bin/bash /home/ec2-user/ga_fetcher/put_data_to_s3.sh `date -d "yesterday" +"%Y%m%d"` > cron_export_ga.log
本來使用這樣的設定,在 bash 下跑過也可以正常執行。
看了 cron 的 log
$ sudo cat /var/log/cron | grep put_data

Feb 16 06:16:01 ip-10-0-3-153 CROND[19830]: (ec2-user) CMD (/bin/bash /home/ec2-user/ga_fetcher/put_data_to_s3.sh `date -d "yesterday" +")
發現 crond 的 command 只抓到 +

其實遇到 % 需要 escape ,在前面加上 \ 解決問題。

$ crontab -l
# export ga
0 0 * * * /bin/bash /home/ec2-user/ga_fetcher/put_data_to_s3.sh `date -d "yesterday" +"\%Y\%m\%d"` > cron_export_ga.log


Feb 16 06:19:01 ip-10-0-3-153 crond[2546]: (ec2-user) RELOAD (/var/spool/cron/ec2-user)
Feb 16 06:19:01 ip-10-0-3-153 CROND[19845]: (ec2-user) CMD (/bin/bash /home/ec2-user/ga_fetcher/put_data_to_s3.sh `date -d "yesterday" +"%Y%m%d"` > cron_export_ga.log)

沒有留言:

張貼留言