简体   繁体   中英

Ubuntu cron not executing, but works fine on command line

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM