简体   繁体   English

Cron使用反引号时出错

[英]Cron error with using backquotes

The following works fine from command line 以下工作正常从命令行

/usr/bin/mysqldump -uUser -pPass Db_name > /var/www/db_backup/db.`date +%Y%m%d%H%M`.sql 

but when I try to do that in cron, I get the error: 但是当我尝试在cron中执行此操作时,我收到错误:

bad ` sign 
errors in crontab file, can't install

I saw someone else on the net solve the same problem by escaping the percent signs, but that didn't help and I tried it with just date inside backquotes with no format specifiers and still got the errors. 我看到网络上的其他人通过逃避百分号来解决同样的问题,但这没有帮助,我尝试了只在反引号内的日期,没有格式说明符,仍然有错误。

I've also seen date's argument enclosed in single or double quotes, but that doesn't help either. 我也看到日期的参数用单引号或双引号括起来,但这也无济于事。

Granted I could just throw it into a script and execute that I suppose - but what fun is that? 当然,我可以把它扔进一个脚本并执行我想的 - 但那有什么乐趣?

Any ideas? 有任何想法吗? I'm using RHEL 5. 我正在使用RHEL 5。

Try it with $() instead of backticks. 尝试使用$()而不是反引号。 And you probably do need to escape the percent signs since cron converts them to newlines otherwise. 你可能确实需要逃避百分号,因为cron将其转换为换行符。

* 0 * * * /usr/bin/mysqldump -uUser -pPass Db_name > /var/www/db_backup/db.$(date +\%Y\%m\%d\%H\%M).sql

Also, you should store the password in an option file with secure permissions (eg. 600 or 640) instead of passing it on the command line. 此外,您应该将密码存储在具有安全权限(例如600或640) 的选项文件中 ,而不是在命令行上传递它。

Put your one line script (as shown) into a proper script file and invoke that from cron: 将您的单行脚本(如图所示)放入正确的脚本文件中并从cron调用它:

$ cat /usr/local/bin/db-backup
#!/bin/sh
/usr/bin/mysqldump -uUser -pPass Db_name > \
   /var/www/db_backup/db.`date +%Y%m%d%H%M`.sql 
$ # use RHEL commands to add db-backup to your crontab

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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