This question already has an answer here:

I am getting an issue while running a cron job for exporting all databases on my Godaddy Shared hosting Cpanel account. But I have trouble finding the syntax error in below command.

Same command is working on my AWS Ec2.

mysql -N -ubackup -pt -e 'show databases' | while read dbname; do mysqldump -ubackup -p123 --complete-insert -N "$dbname" > /home/test/sqlbackups/'$(date +"%Y-%m-%d")-$dbname'.sql;done

Error Received on Mail:

/bin/bash: -c: line 0: unexpected EOF while looking for matching `''
/bin/bash: -c: line 1: syntax error: unexpected end of file

marked as duplicate by Gilles bash Sep 6 '17 at 21:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Crontab commands will have all unescaped occurrences of % replaced by newlines. This is from the crontab(5) manual on my system:

The command field (the rest of the line) is the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab. Percent signs (%) in the command, unless escaped with a backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input.

Your crontab command should look like

mysql -N -ubackup -pt -e 'show databases' | while read dbname; do mysqldump -ubackup -p123 --complete-insert -N "$dbname" > /home/test/sqlbackups/"$(date +"\%Y-\%m-\%d")-$dbname".sql;done

Here, I've also corrected the $(...) that was previously single-quoted (and thus not expanded by the shell).

In general, it's better to put all non-trivial cron jobs in their own scripts and then schedule these instead. That way you have more control over things like this and you are also able to choose the correct interpreter for any particular job (e.g. ksh or bash instead of sh). It additionally makes any subject lines in emails sent to you from the cron daemon more readable.

  • 1
    Using scripts instead of long command lines in cron jobs also makes the subject of emails you get about them failing more legible – Stéphane Chazelas Sep 6 '17 at 10:38
  • Thanks it works. I will keep it mind while adding date formats in future. – axcl Sep 6 '17 at 10:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.