LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-30-2009, 06:36 PM   #1
Techno Guy
Member
 
Registered: Dec 2008
Posts: 62

Rep: Reputation: 15
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 "%"

Last edited by Techno Guy; 05-30-2009 at 06:37 PM.
 
Old 05-30-2009, 06:50 PM   #2
nilleso
Member
 
Registered: Nov 2004
Location: ON, CANADA
Distribution: ubuntu, RHAS, and other unmentionables
Posts: 372

Rep: Reputation: 31
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
 
Old 05-30-2009, 07:00 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 05-30-2009, 09:34 PM   #4
Techno Guy
Member
 
Registered: Dec 2008
Posts: 62

Original Poster
Rep: Reputation: 15
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").
 
Old 05-31-2009, 02:07 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Techno Guy View Post
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"
 
Old 05-31-2009, 06:35 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.

Last edited by chrism01; 05-31-2009 at 06:54 PM.
 
Old 05-31-2009, 06:48 PM   #7
Techno Guy
Member
 
Registered: Dec 2008
Posts: 62

Original Poster
Rep: Reputation: 15
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cron job doesnt work, it works manually scofiled83 Programming 8 05-30-2009 03:12 PM
shell prompt works, cron job fails btuley Linux - General 1 11-13-2008 11:40 AM
Errors when running Perl script in a cron job meshach Slackware 2 06-20-2006 09:10 PM
log cron job to capture errors if there are ashley75 Linux - General 1 05-21-2004 10:34 AM
cron job errors existo Slackware 4 01-23-2004 02:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration