1

My cronjob does not work. When it is executed, it gets this error-mail:

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

My crontab:

# Delete logfile from last week
00 21 * * * rm /var/log/dumping_$(date -d @$(( $(date "+%s") + 86400 )) "+%u").log

I tried several options, e.g.

00 21 * * * 'rm /var/log/dumping_$(date -d @$(( $(date "+%s") + 86400 )) "+%u").log'

00 21 * * * /bin/bash -c 'rm /var/log/dumping_$(date -d @$(( $(date "+%s") + 86400 )) "+%u").log'

None of them worked as cronjob. It works when I enter it on command-line directly. What's the correct syntax?

Pat Myron
  • 129

2 Answers2

3

% has a special meaning in cronjobs – it means the text afterwards is given as stdin to the command before it. To use a literal percent sign, either try \escaping it or just move everything into an external script and call that from crontab.

(IMHO, when it nests $(…) more than twice, it's a sure sign it should be extracted into a script.)

Note that you can also use date -d tomorrow +%u instead of calculating it manually.

grawity
  • 501,077
0

The main difference is that your shell, when running it manually, is most likely bash, while cron defaults to sh. I cannot say for sure that this is the source of the problem, but seeing as your line is somewhat complex (as in, loads of special characters that may or may not be interpreted differently between various shells and interpreters), you might benefit from sticking the rm command with all its arguements into a script, and calling the script from cron. At least that would be the first thing i'd try.

Jarmund
  • 6,277
  • 5
  • 38
  • 60