dongxu1668 2014-01-09 16:21
浏览 151
已采纳

Ubuntu cron没有执行,但在命令行上工作正常

I have a backup cron job that is supposed to run once a week. It works perfectly fine when I execute it on the command line, but it never executes in cron. My other cron tasks execute without issue. I assume it must have something to do with my options or incorrect syntax. Here is the cron entry in question:

10 10 * * 0 php ~/v2/symfony/symfony cc; tar -czf ~/backups/schroeder/v2/v2_site_backup_`date +%Y-%m-%d_%H-%M`.tgz ~/v2/symfony | tee -a ~/log/weekly_backup.log

SOLUTION Turns out I needed to escape the % when inputting it my cron list. So the command that works is:

10 10 * * 0 php ~/v2/symfony/symfony cc; tar -czf ~/backups/schroeder/v2/v2_site_backup_`date +\%Y-\%m-\%d_\%H-\%M`.tgz ~/v2/symfony | tee -a ~/log/weekly_backup.log
  • 写回答

1条回答 默认 最新

  • dongleiqiao2107 2014-01-09 16:22
    关注

    crontab has a very small environment, so you have to indicate full paths.

    10 10 * * 0 /path/of/php /home/your_user/v2/symfony/symfony cc; tar -czf /home/your_user/backups/schroeder/v2/v2_site_backup_`date +%Y-%m-%d_%H-%M`.tgz /home/your_user/v2/symfony | tee -a /home/your_user/log/weekly_backup.log
               ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^                                 ^^^^^^^^^^^^^^^                                                                 ^^^^^^^^^^^^^^^                     ^^^^^^^^^^^^^^^
    

    instead of

    10 10 * * 0 php ~/v2/symfony/symfony cc; tar -czf ~/backups/schroeder/v2/v2_site_backup_`date +%Y-%m-%d_%H-%M`.tgz ~/v2/symfony | tee -a ~/log/weekly_backup.log
    

    That is, change all ~ for /home/your_user/.

    Also! Escape each % as read here:

    date +%Y-%m-%d_%H-%M
    

    has to be

    date +\%Y-\%m-\%d_\%H-\%M
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化