开发者

Cron error with using backquotes

开发者 https://www.devze.com 2023-01-10 17:21 出处:网络
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

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:

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 开发者_StackOverflow中文版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.


Try it with $() instead of backticks. And you probably do need to escape the percent signs since cron converts them to newlines otherwise.

* 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.


Put your one line script (as shown) into a proper script file and invoke that from 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
0

精彩评论

暂无评论...
验证码 换一张
取 消