32

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
  • 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
69

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`"
|improve this answer|||||
9

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

|improve this answer|||||
4

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

|improve this answer|||||
  • 1
    +1 This is the favoured syntax nowadays anyway. – Dan Carley Nov 13 '09 at 17:05
  • 4
    but it has nothing to do with the user's question. – Aaron Brown Nov 13 '09 at 18:53
  • 1
    of course it has, its about backsticks, read the question again. – Sergio Abreu Feb 3 '17 at 14:58
  • Thanks, it took me a while to realize the backticks were breaking my crons - I've started using $() in all my scripts now too – Eric Kigathi Mar 7 '17 at 19:23
-2

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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