开发者

Syntax error only when command is run from cron

开发者 https://www.devze.com 2023-01-08 06:10 出处:网络
This command: /usr/bin/mysqldump --add-drop-table -u myuser -pmypass mydb > \"/home/myuser/dbBackups/\"`date +%Y%m%d`\".sql\"

This command:

/usr/bin/mysqldump --add-drop-table -u myuser -pmypass mydb > "/home/myuser/dbBackups/"`date +%Y%m%d`".sql"

works fine from the command line but whenb cron runs it I get

/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax erro开发者_StackOverflow社区r: unexpected end of file

The command is all on one line in the crontab as well so I'm confused by the line 0 and line 1 references...

Can anyone advise me as to what I am doing wrong there?


It's the obvious dumb question, but do you have the matching backquote in your crontab (crontab -l)?

The line one, line zero stuff isn't referring to the lines in the crontab, only to the 'lines' in the one-line script.

Updated:

Ah, I think I've got it. This is from crontab(5):

Percent-signs (%) in the command, unless escaped with backslash (\),
will be changed into newline characters, and all data after the
first % will be sent to the command as standard input.

So the percent characters in your date spec are being interpreted as newlines, which means the backquote isn't terminated before the newline, which would produce your error message.

So escape the percent characters. I'd forgotten that about crontab....


The easiest fix is probably to put the whole command in a shell script and just have that be run. So make a scriptName.sh file that contains the command you listed and have crontab call that script. That gets around all these odd problems.


Commands executed from cron do not have access to the environment variables from your login shell, including the path. So try the following (adding fully qualified path to date):

/usr/bin/mysqldump --add-drop-table -u myuser -pmypass mydb > "/home/myuser/dbBackups/"`/usr/bin/date +%Y%m%d`".sql"

Of course, verify if your date command is located elsewhere by running which date then adjust the path if necessary.

0

精彩评论

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

关注公众号