I have a crontab set up that errors out every time I attempt to do it. It works fine in the shell. It's the format I'm using when I attempt to automatically insert the date into the filename of the database backup. Does anyone know the syntax I need to use to get cron to let me insert the date into the filename?
我有一个crontab设置,每次尝试时都会出错。它在shell中运行良好。当我尝试将日期自动插入数据库备份的文件名时,这是我正在使用的格式。有谁知道我需要用来让cron让我在文件名中插入日期的语法?
mysqldump -hServer -uUser -pPassword Table | gzip >
/home/directory/backups/table.$(date +"%Y-%m-%d").gz
Thanks in advance!
提前致谢!
2 个解决方案
#1
17
What about something like this for the "command" part of the crontab :
对于crontab的“命令”部分,这样的事情怎么样:
mysqldump --host=HOST --user=USER --password=PASSWORD DATABASE TABLE | gzip > /tmp/table.`date +"\%Y-\%m-\%d"`.gz
What has changed from OP is the escaping of the date format :
从OP改变的是逃避日期格式:
date +"\%Y-\%m-\%d"
(And I used backticks -- but that should do much of a difference)
(而且我使用了反引号 - 但这应该有很大的不同)
(Other solution would be to put your original command in a shell-script, and execute this one from the crontab, instead of the command -- would probably be easier to read/write ^^)
(其他解决方案是将原始命令放在shell脚本中,并从crontab执行此命令,而不是命令 - 可能更容易读/写^^)
#2
0
The most typical reason for "works in shell but not in cron" is that commands you try to execute are not in PATH.. Reason is that shell invoked from cron aint loading same files as your login shell.
“在shell中工作但在cron中工作”的最典型原因是您尝试执行的命令不在PATH中。原因是从cron aint调用的shell加载与登录shell相同的文件。
Fix: add absolute path to each command you try to execute.
修复:为您尝试执行的每个命令添加绝对路径。
Second thing i notice in your command. Syntax for running your date command looks like its not very portable. Change that to be in backticks, or run put your whole command to shellscript (also, you can use it to set your path too) and execute that script from cron..
我在你的命令中注意到的第二件事。运行date命令的语法看起来不太便于携带。将其更改为反引号,或运行将整个命令放到shellscript(也可以使用它来设置路径)并从cron执行该脚本。
EDIT:
During the writing my original reply my keyboard layout didnt have backticks so check what Pascal wrote.
在写我的原始回复期间,我的键盘布局没有反复,所以检查Pascal写的是什么。
And example of what you could do with a shellscript:
以及使用shellscript可以执行的操作的示例:
Copy following to /usr/local/bin/dumptable.sh
将以下内容复制到/usr/local/bin/dumptable.sh
#!/bin/sh
/usr/bin/mysqldump --host=HOST --user=USER --password=PASSWORD DATABASE TABLE | /bin/gzip > /tmp/table.`/bin/date +"\%Y-\%m-\%d"`.gz
and then put the the /usr/local/bin/dumptable.sh into cron..
然后将/usr/local/bin/dumptable.sh放入cron ..
#1
17
What about something like this for the "command" part of the crontab :
对于crontab的“命令”部分,这样的事情怎么样:
mysqldump --host=HOST --user=USER --password=PASSWORD DATABASE TABLE | gzip > /tmp/table.`date +"\%Y-\%m-\%d"`.gz
What has changed from OP is the escaping of the date format :
从OP改变的是逃避日期格式:
date +"\%Y-\%m-\%d"
(And I used backticks -- but that should do much of a difference)
(而且我使用了反引号 - 但这应该有很大的不同)
(Other solution would be to put your original command in a shell-script, and execute this one from the crontab, instead of the command -- would probably be easier to read/write ^^)
(其他解决方案是将原始命令放在shell脚本中,并从crontab执行此命令,而不是命令 - 可能更容易读/写^^)
#2
0
The most typical reason for "works in shell but not in cron" is that commands you try to execute are not in PATH.. Reason is that shell invoked from cron aint loading same files as your login shell.
“在shell中工作但在cron中工作”的最典型原因是您尝试执行的命令不在PATH中。原因是从cron aint调用的shell加载与登录shell相同的文件。
Fix: add absolute path to each command you try to execute.
修复:为您尝试执行的每个命令添加绝对路径。
Second thing i notice in your command. Syntax for running your date command looks like its not very portable. Change that to be in backticks, or run put your whole command to shellscript (also, you can use it to set your path too) and execute that script from cron..
我在你的命令中注意到的第二件事。运行date命令的语法看起来不太便于携带。将其更改为反引号,或运行将整个命令放到shellscript(也可以使用它来设置路径)并从cron执行该脚本。
EDIT:
During the writing my original reply my keyboard layout didnt have backticks so check what Pascal wrote.
在写我的原始回复期间,我的键盘布局没有反复,所以检查Pascal写的是什么。
And example of what you could do with a shellscript:
以及使用shellscript可以执行的操作的示例:
Copy following to /usr/local/bin/dumptable.sh
将以下内容复制到/usr/local/bin/dumptable.sh
#!/bin/sh
/usr/bin/mysqldump --host=HOST --user=USER --password=PASSWORD DATABASE TABLE | /bin/gzip > /tmp/table.`/bin/date +"\%Y-\%m-\%d"`.gz
and then put the the /usr/local/bin/dumptable.sh into cron..
然后将/usr/local/bin/dumptable.sh放入cron ..