bash - 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
解决方案
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
推荐阅读
- html - Chrome 开发工具移动版。无法使用 100% 的移动视图
- python - 在 Pycharm IDE 中使用停止按钮停止脚本会引发 KeyboardInterrupt 错误
- javascript - 我如何只在 React JS 中重新渲染“Like”按钮?
- python - 开放式健身房中多离散动作空间的阶梯函数
- javascript - TypeError:cb不是nodejs中的函数?
- protractor - 使用 Saucelabs (Chrome 90) 上传的量角器文件上传后重定向到“data:text/html”,在本地工作,但在 Saucelabs 上不起作用
- python - 我正在尝试制作一个通过命令播放 mp3 文件的机器人,但它无法正常工作且没有错误
- arrays - IOArray 和 STArray 的奇怪分析开销
- azure - 新邮件到达时 Office 365 电子邮件触发器未按 Azure 逻辑应用的要求触发
- php - 循环时累积每月总计