6

I can use this command

mysqldump -u"root"  myDB| gzip > mydb_`date +%d-%m-%Y`.sql.gz

but when run in crontab

* * * * * mysqldump -u"root"  myDB| gzip > mydb_`date +%d-%m-%Y`.sql.gz

( this error cause by function date, when i remove it , crontab run good )

on ubuntu, it happen this error in log file.

ubuntu CRON[xxxx] (user) CMD(mysqldump -u"root"  myDB| gzip > mydb_`date+)
ubuntu CRON[xxxx] (CRON) error ( grandchild #5353 failed with exit status 2)
ubuntu CRON[xxxx] (CRON) info (no MTA installed, discarding output)
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
meotimdihia
  • 4,191
  • 15
  • 49
  • 69
  • 1
    Your crontab is not the same command you are running from the command line. You omitted the `date +%d-%m-%Y` (noting the lack of backtics) which are what make that actually work, as the backtics execute and return the result of the date .... function. – gview Aug 27 '11 at 02:30
  • it is same but i can't write out with stackoverflow . – meotimdihia Aug 27 '11 at 02:42
  • Yes you can. Use backticks to format part of a line as code; indent by 4 or more spaces to format one or more lines as code. Select the range of text and click the `{}` icon to do this automatically. Or you can escape a backtick with a backslash: `\``. – Keith Thompson Aug 27 '11 at 02:59

2 Answers2

13

% signs in a crontab command are converted to newlines, and all data after the first % is sent to the command's stdin. Replace each % with \%.

(And you only had 4 time fields: * * * *; you need 5 (you later fixed the question).)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
0

From the man 5 crontab:

The ``sixth'' field (the rest of the line) specifies 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 file.

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. There is no way to split a single command line onto multiple lines, like the shell's trailing "\".

Benjamin
  • 3,217
  • 2
  • 27
  • 42