Ubuntu Server Cron Jobs 未运行

Ubuntu Server Cron Jobs 未运行

我快要崩溃了:我搞不懂为什么我的 crontab -e 文件中的作业没有运行。没有一个作业运行,所以“最后一个 cron 作业没有运行”的问题不适用。我还检查了线程,没有发现任何错误。我甚至去掉了百分号。

这是我在 root 用户下运行时的 crontab -e 文件:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# Minute Stunde TagIMonat Monat TagIWoche Kommando
# m h  dom mon dow   command
50 3 * */2 * /usr/bin/openssl pkcs12 -export -out /home/user/export.pfx -inkey /etc/letsencrypt/live/domain/privkey.pem -in /etc/letsencrypt/live/domain/chain.pem -password pass: blabla > /var/log/cron.log
51 3 * */2 * /usr/bin/sshpass -p xxx scp /home/user/export.pfx [email protected]:/path/to/file/> /var/log/cron.log
0 0 */5 * * /usr/bin/rsync -avx /var/www/nextcloud/apps /nextcloud/backup/path/nextcloud-apps_`date +"\%Y\%m\%d"`/ > /var/log/cron.log
0 0 */5 * * /usr/bin/rsync -avx /var/www/nextcloud/config/config.php /nextcloud/backup/path/nextcloud-config_`date +"\%Y\%m\%d"`/ > /var/log/cron.log
0 0 * * * /usr/bin/mysqldump --single-transaction -h localhost -u user -pPassword db > /nextcloud/backup/path/nextcloud-sqlbkp_`date + "\%Y\%m\%d"`.sql > /var/log/cron.log

也许有人可以帮助我解决我的问题。

谢谢

答案1

天啊 :)

首先,你不应该直接将如此长而复杂的一行代码粘贴到 cron 中。相反,你应该为所有这些命令创建 bash 脚本。

即你可以替换:

0 0 */5 * * /usr/bin/rsync -avx /var/www/nextcloud/apps /nextcloud/backup/path/nextcloud-apps_`date +"\%Y\%m\%d"`/> /var/log/cron.log
0 0 */5 * * /usr/bin/rsync -avx /var/www/nextcloud/config/config.php /nextcloud/backup/path/nextcloud-config_`date +"\%Y\%m\%d"`/ > /var/log/cron.log

0 0 */5 * * /root/bin/rsync_cronjobs.sh > /var/log/cron.log

并将命令移到 bash 脚本中,即/root/bin/rsync_cronjobs.sh

/bin/bash #!/bin/bash

DATE=`日期+%Y%m%d`

/usr/bin/rsync -avx /var/www/nextcloud/apps /nextcloud/backup/path/nextcloud-apps_${DATE}/
/usr/bin/rsync -avx /var/www/nextcloud/config/config.php /nextcloud/backup/path/nextcloud-config_${DATE}`/

看起来更干净吧?

另外,请确保你了解这些 cronjob 的运行频率。你可以使用以下命令检查它们克隆特夫。为了安全起见,切勿将任何私人信息(如密码)粘贴到此类工具中。

相关内容