我为 root 用户设置了以下 crontab:
root@curie:/usr/home/username # crontab -l
# /etc/crontab - root's crontab for FreeBSD
#
# $FreeBSD: release/10.0.0/en_US.ISO8859-1/books/handbook/config/chapter.xml 43126 2013-11-07 16:37:11Z gabor $
#
#
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
#
#minute hour mday month wday who command
#
41 13 * * * root ( /usr/local/bin/mysqldump --default-character-set=utf8mb4 rt5 --tables sessions --no-data --single-transaction ; /usr/local/bin/mysqldump --default-character-set=utf8mb4 rt5 --ignore-table rt5.sessions --single-transaction ) | /usr/bin/gzip > /home/username/rt-db-backups/rt-`/bin/date +\%Y\%m\%d`.sql.gz
rt-20210916.sql.gz
该作业似乎正在运行,但未创建预期的文件:
# ls -la rt-db-backups/
total 5392
drwxr-xr-x 2 username username 512 Sep 16 13:40 .
drwxr-xr-x 5 username username 512 Sep 15 14:57 ..
-rw-r--r-- 1 root username 2693630 Sep 16 10:25 rt-20210915.sql.gz
我尝试通过检查日志进行调查,/var/run/cron
但它表明该命令正在运行:
# tail /var/log/cron
Sep 16 13:40:00 curie /usr/sbin/cron[7387]: (root) CMD (/usr/libexec/atrun)
Sep 16 13:40:34 curie crontab[7390]: (root) BEGIN EDIT (root)
Sep 16 13:40:42 curie crontab[7390]: (root) REPLACE (root)
Sep 16 13:40:43 curie crontab[7390]: (root) END EDIT (root)
Sep 16 13:41:00 curie /usr/sbin/cron[876]: (root) RELOAD (tabs/root)
Sep 16 13:41:00 curie /usr/sbin/cron[7396]: (root) CMD (root ( /usr/local/bin/mysqldump --default-character-set=utf8mb4 rt5 --tables sessions --no-data --single-transaction ; /usr/local/bin/mysqldump --default-character-set=utf8mb4 rt5 --ignore-table rt5.sessions --single-transaction ) | /usr/bin/gzip > /home/username/rt-db-backups/rt-`/bin/date +%Y%m%d`.sql.gz)
Sep 16 13:41:32 curie crontab[7403]: (root) LIST (root)
Sep 16 13:44:00 curie /usr/sbin/cron[7406]: (operator) CMD (/usr/libexec/save-entropy)
Sep 16 13:45:00 curie /usr/sbin/cron[7415]: (root) CMD (/usr/libexec/atrun)
似乎()
和{}
符号在 cron 中不起作用?
我怎样才能编写一个实际上与以下内容相同的 cron 命令:
( /usr/local/bin/mysqldump --default-character-set=utf8mb4 rt5 --tables sessions --no-data --single-transaction ; /usr/local/bin/mysqldump --default-character-set=utf8mb4 rt5 --ignore-table rt5.sessions --single-transaction ) | /usr/bin/gzip > /home/username/rt-db-backups/rt-`/bin/date +%Y%m%d`.sql.gz
请注意,我尝试了以下方法,但也不起作用:
{ /usr/local/bin/mysqldump --default-character-set=utf8mb4 rt5 --tables sessions --no-data --single-transaction ; /usr/local/bin/mysqldump --default-character-set=utf8mb4 rt5 --ignore-table rt5.sessions --single-transaction ; } | /usr/bin/gzip > /home/username/rt-db-backups/rt-`/bin/date +\%Y\%m\%d`.sql.gz
答案1
@steeldriver 是正确的。我使用了错误的格式,因为 /etc/crontab 与 root 用户的 crontab (不包含 who 字段)不同。