40

Here's what I'd like to automate:

00 08 * * * psql -Uuser database < query.sql | mail somone@null.com -s "query for `date +%Y-%m-%dZ%I:%M`"

Here's the error message:

/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
Terry G Lorber
  • 565
  • 2
  • 7
  • 12
  • 2
    Apart from the giving error consider to put this in a schell script. It will pretend the crontab to be clumsy and you can add comments and config to your script file. – PeterMmm Nov 13 '09 at 15:09

4 Answers4

80

From crontab(5):

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

Just add backslashes before % signs:

00 08 * * * psql -Uuser database < query.sql | mail somone@null.com -s "query for `date +\%Y-\%m-\%dZ\%I:\%M`"
Juliano
  • 5,402
  • 27
  • 28
14

To resolve the issue escape your % characters. date +%Y-%m-%d should be date +\%Y-\%m-\%d

Unfortunately this answer is a little late but the issues is not PATH or backticks - the issue is simply that the percent character '%' is a special character used to denote a NEWLINE or an STDIN in crontab entries.

This conflicts with the formatting input of the date command. As such a command that includes: date +%Y-%m-%d will be interpreted as:

date Y- m- d

Eric Kigathi
  • 336
  • 3
  • 6
6

I had a lot of problems with backticks also. Sometimes you need more than one occurrence of quotes and backticks. Just replace them for $().

Example:

export NOW=`date`
by
export NOW=$(date)

-Gilson Soares

Guilsson
  • 185
  • 2
-1

It has nothing to do with backsticks. A terrible thing is that cron DO NOT see the PATH, and you MUST tell cron "what PATH is ?" over and over in the cron scripts.

PATH=/bin:/usr/bin:/usr/sbin
* * * * * /your/script/here

And I am in favour of writing the command in a file instead of passing the command literally in the cron line. It is much more elegant to have

* * * * * /your/script/here

than

* * * * * perl -e '$@#$@$%%@' | grep -e '@#$@$#$@' | sed s/asfdf/asdfa/

these line can go inside one file, chmod +x file, and this file to be invoked.

Sergio Abreu
  • 107
  • 2