首页 > 解决方案 > 2 个 cronjobs 中的 1 个未在 Ubuntu 16.04 for windows 中运行 - 由于 cron 中的“日期”扩展

问题描述

我的 crontab 中有两个 cronjobs。第一个作品;第二个没有。具体来说,

40 18 * * * cp /mnt/c/Users/Adrian/pick_of_the_day/daily_winner /mnt/c/Users/Adrian/pick_of_the_day/`date +'%b-%d-winner'` 

不起作用。

我努力了:

- commenting out the first one
- removing the full path for the destination file and checking the default DIR for my user
- using a relative path for the dest file
- looking through 5 other cronjob questions on this site
- looking for the cron log file but it doesn't exist.  Before someone tells me to check syslog, please note it doesn't exist on my installation.
- **I removed the ">>/var/log/cron_output.log 2>&1" and just left the command**

Aviator@AW:/var/log$ ls alternatives.log btmp dpkg.log fsck lxd upstart apt dist-upgrade fontconfig.log lastlog unattended-upgrades wtmp Aviator@AW:/var/log$

35 18,21 * * * /mnt/c/Users/Adrian/pick_of_the_day/winner
40 18 * * * cp /mnt/c/Users/Adrian/pick_of_the_day/daily_winner 
/mnt/c/Users/Adrian/pick_of_the_day/`date +'%b-%d-winner'` >>/var/log/cron_output.log 2>&1

标签: bashcronwindows-subsystem-for-linux

解决方案


cron 中的 % 字符需要转义,而如果在脚本中或直接在 cmd 行上则不需要转义。

逃脱他们后,cronjob 运行如预期。

cp /mnt/c/Users/Adrian/pick_of_the_day/daily_winner /mnt/c/Users/Adrian/pick_of_the_day/$(date +'\%b\%mwinner')

我刚刚找到的相关答案: https ://unix.stackexchange.com/questions/29578/how-can-i-execute-date-inside-of-a-cron-tab-job

https://unix.stackexchange.com/questions/252615/why-doesnt-date-ft-work-in-crontab


推荐阅读