1

I am fighting with a cronjob which does not work. Everytime when it is executed it get an error-mail saying

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

My crontab is this one:

# 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. Of course, it is working when I enter it on command-line directly. What would be the correct syntax?

2 Answers 2

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.

1
  • Thank you, 00 21 * * * rm /var/log/mediation/dumping_`date -d tomorrow +\%u`.log did it. Jul 1, 2016 at 7:22
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .