LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Keep getting errors when running cron job that works in normal terminal. (https://www.linuxquestions.org/questions/linux-newbie-8/keep-getting-errors-when-running-cron-job-that-works-in-normal-terminal-729573/)

Techno Guy 05-30-2009 07:36 PM

Keep getting errors when running cron job that works in normal terminal.
 
Im trying to put the date onto the output filename which works fine when I try it in terminal.
But when I add it to the crontab it keeps giving me these errors


This is what I put into the crontab (on my Mac system)
Code:

1 1 * * * /test.sh >> "/testDate $(date "+%d-%m-%y").txt"
This is what I get when the cron job runs:
Code:

Subject: Cron <test@testUser-2> /test.sh >> "/test $(date "+
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
Date: Sun, 31 May 2009 01:01:00

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

Any ideas why this is happening? (apart from the fact that it doesn't go any further then the first "%"

nilleso 05-30-2009 07:50 PM

really?... that works from the command line?
try

1 1 * * * /test.sh >> "/testDate `date +%d-%m-%y`.txt"

(note those aren't single quotes around the date command - they're backticks)

cheers :)

colucix 05-30-2009 08:00 PM

The % sign has a special meaning in crontab (see man crontab to find out). You have to escape it:
Code:

1 1 * * * /test.sh >> /testDate/$(date +\%d-\%m-\%y).txt
Also you don't need all these quotes (I removed them all) and maybe you missed a slash between /testdate and the filename.

Techno Guy 05-30-2009 10:34 PM

Thanks for the fast replies!

I will add the backticks, and see if that helps, thanks Nilleso.

Oh and @colucix, I have the quotes so that I can have a space between "testDate" and the date (eg. "testDate 30-05-09").

colucix 05-31-2009 03:07 AM

Quote:

Originally Posted by Techno Guy (Post 3557859)
Oh and @colucix, I have the quotes so that I can have a space between "testDate" and the date (eg. "testDate 30-05-09").

Ok for the quotes (I didn't understand since I ususally don't put spaces in the file names). Regarding the backticks they are equivalent to the $(...) syntax, or better, the $(...) construct is more advanced.

As I told above the problem is the percent sign, as stated from the man page:
Quote:

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.
You can finally end up with the following cron job:
Code:

1 1 * * * /test.sh >> "/testDate $(date +\%d-\%m-\%y).txt"

chrism01 05-31-2009 07:35 PM

Also, although spaces in filenames are technically legal, most *nix utils/programs use <space> as an argument separator, so will fail without special handling.
It's much easier not to use spaces in filenames.
In fact, I'd be surprised if any files installed by the orig installer had spaces ... there's a reason/hint.

Techno Guy 05-31-2009 07:48 PM

Yeah I know that spaces shouldn't be used, and 99% of the time my filenames are all without spaces, but this particular cron job is run on my daily computer (not my server) therefor I would like to keep spaces in my file names for this system, but yes on my server all filenames are without spaces.

PS.
My cron job ran this morning and the log files was created perfectly, I used this in the end:
Code:

1 1 * * * /test.sh >> "/test `date +\%d-\%m-\%y`.txt"
Thanks again for all your help!


All times are GMT -5. The time now is 12:24 AM.