views:

33

answers:

3

Hi folks,

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 error: 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?

TIA!!

JG

+1  A: 

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.

Amardeep
This is an excellent point I will bear in mind in the future. Did not solve this issue but I definitely should have made sure the full path was included there.
jerrygarciuh
+4  A: 

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

Norman Gray
Sure do; the cmd above is exactly it minus the actual un/pw/db name.
jerrygarciuh
+2  A: 

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.

Paul Rubel
Thanks, this did the trick!
jerrygarciuh