Linux Cron 说“crontab 文件中的错误,无法安装”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14763687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cron says "errors in crontab file, cannot install"
提问by Steven Sokulski
I'm attempting to execute the following series of commands to create backups of MySQL databases.
我正在尝试执行以下一系列命令来创建 MySQL 数据库的备份。
When I attempt to add the command to my crontab using crontab -e
I get the error "errors in crontab file, cannot install" and asks me if I want to retry.
当我尝试使用该命令将命令添加到我的 crontab 时crontab -e
,出现错误“crontab 文件中的错误,无法安装”并询问我是否要重试。
mkdir /home/mysql-backup/`date '+%m-%d-%Y'`; mysql -s -r -e 'show databases' | while read db; do mysqldump $db -r /home/mysql-backup/`date '+%m-%d-%Y'`/${db}.sql; done; rm -r -f `date --date="1 week ago" +%m-%d-%Y`; du -k |sort -n > output; mail -s "MySQL Backups" "[email protected]" < output
Is there anything I should be changing in this file? Or should I look into creating a script file and calling that from cron.
在这个文件中有什么我应该改变的吗?或者我应该考虑创建一个脚本文件并从 cron 调用它。
Thanks in advance for any assistance you can provide.
预先感谢您提供的任何帮助。
回答by Scott
Yes; as a matter of style, if nothing else, I encourage to put the SQL commands into a shell script, and?then run the shell script from cron
.??(And, as Anew points out, the command sequence is easier to?maintain/debug if it's broken out into multiple lines, with comments.)? But –– is that all of what you're feeding into crontab
?? Look at man crontab
and add the fields that specify when you want the command to run.
是的; 作为风格问题,如果不出意外,我鼓励将 SQL 命令放入 shell 脚本中,然后从cron
.??运行 shell 脚本(而且,正如 Anew 指出的,命令序列更容易维护/如果将其分解为多行并带有注释,请进行调试。)?但是——这就是你要喂的所有东西吗crontab
??查看man crontab
并添加指定何时运行命令的字段。
回答by fvu
If you gave that script to crontab -e
of course it will disagree. A line in a crontab file should start with 5 fields indicating when you want the script to run, as can be read in crontab's manpage.
如果你给那个脚本crontab -e
当然它会不同意。crontab 文件中的一行应该以 5 个字段开头,指示您希望脚本何时运行,这可以在 crontab 的联机帮助页中阅读。
On the other hand, most Linux distros nowadays have preset facilities for things that should be executed hourly (/etc/cron.hourly), daily (/etc/cron.daily), etc. It's a whole lot easier to just put your script in a file in the appropriate directory and it will get executed in the selected time raster. An added advantage is that in these files you won't be forced to cram everything into one line.
另一方面,现在大多数 Linux 发行版都为应该每小时 (/etc/cron.hourly)、每天 (/etc/cron.daily) 等执行的事情预先设置了设施。只需放置脚本就容易多了在适当目录中的文件中,它将在选定的时间栅格中执行。一个额外的好处是,在这些文件中,您不会被迫将所有内容都塞进一行。
回答by Geoff Gustafson
From the crontab(5)man page, it looks like percent signs (%) have a special meaning, so that is probably where you're running into trouble.
从crontab(5)手册页看,百分号 (%) 似乎具有特殊含义,因此这可能是您遇到麻烦的地方。
Yes, you should put your commands into a separate shell script and just call that from the crontab line. This will also make it much easier to read the crontab file, and you can format your script nicely so that it's easier to maintain. And you can test it separately from the crontab that way.
是的,你应该把你的命令放到一个单独的 shell 脚本中,然后从 crontab 行调用它。这也将使阅读 crontab 文件变得更加容易,并且您可以很好地格式化您的脚本,以便更容易维护。您可以通过这种方式与 crontab 分开测试。