LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-23-2007, 08:51 AM   #1
kashyapvirgo
LQ Newbie
 
Registered: Feb 2007
Posts: 13

Rep: Reputation: 0
cron job question


I just try to put date for log file name with my backup script in cron job but it doesnt work.

here is line

0 1 * * 1-6 /usr/BackupScriptsForCronjob/CCFullBackup.sh > /usr/BackupScriptsForCronjob/`date +%y%m%d-%a`-log.txt

but above text does not generate any file name with DATE-log.txt.if i put just log.txt it gives output in file called log.txt.


When i do it manually it works completely fine either way.
 
Old 02-23-2007, 09:10 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Can you give the complete and exact crontab line that does work? The one where you "just put log.txt"?
 
Old 02-23-2007, 10:35 AM   #3
andrews-mark
Member
 
Registered: Feb 2007
Location: London
Distribution: debian
Posts: 108

Rep: Reputation: 15
I'm not sure exactly why it is doing what it is doing but I encountered the same problem as you. It is easy to fix though.

As an experiment, I set up the following cron job
Code:
*/2 * * * * echo `date` > junk/`date +%y%m%d-%a-%H-%M-%S`-log.txt
so that every 2 mins it would write the current date to a file in my junk directory with name made up from the present date and time.

It did not work. I'm not sure why. Maybe someone smarter than me will tell me why.

Anyhow, it is easy to fix. I just put
echo `date` > junk/`date +%y%m%d-%a-%H-%M-%S`-log.txt
into a file called prog.sh, gave it executable permission and created the cron job
Code:
*/2 * * * * /home/andrews/prog.sh
it works just as it should
Code:
andrews@centurian ~/junk> ls -1t 
070223-Fri-16-32-01-log.txt
070223-Fri-16-30-01-log.txt
070223-Fri-16-28-01-log.txt
070223-Fri-16-26-02-log.txt
070223-Fri-16-24-01-log.txt
070223-Fri-16-22-01-log.txt
070223-Fri-16-20-01-log.txt
070223-Fri-16-18-01-log.txt
andrews@centurian ~/junk> cat 070223-Fri-16-32-01-log.txt 

-mark


Fri Feb 23 16:32:01 GMT 2007
 
Old 02-23-2007, 11:44 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
The solution from andrews-mark is fine: better to put commands inside a script and execute that script as cron job. This will avoid (almost) any problem with the cron environment and a lot of headache!
BTW, the problem you encountered is due to the special meaning of the % sign in crontabs. Putting a % in a crontab entry, means to start a newline. Indeed, the standard error from cron reports
Code:
/bin/sh: -c: line 0: unexpected EOF while looking for matching '`'
/bin/sh: -c: line 1: syntax error: unexpected end of file
As you can see, the command has been splitted into two lines. In line 0 bash cannot find the matching closing quote. In line 1 bash expects something more because encountered a starting quote. In other words, crontab represent the command as
Code:
/usr/BackupScriptsForCronjob/CCFullBackup.sh > /usr/BackupScriptsForCronjob/`date +
y%m%d-%a`-log.txt
Note that only the first % sign has the meaning of newline. I think (but not for sure) that the stuff after the first % is used as standard input from the previous command.
Now, let's see the workaround! To avoid this behaviour you have to simply escape every % sign with a backslash in front of it
Code:
0 1 * * 1-6 /usr/BackupScriptsForCronjob/CCFullBackup.sh > /usr/BackupScriptsForCronjob/`date +\%y\%m\%d-\%a`-log.txt
Sorry for the long preamble!

Last edited by colucix; 02-23-2007 at 12:00 PM.
 
Old 02-23-2007, 12:02 PM   #5
kashyapvirgo
LQ Newbie
 
Registered: Feb 2007
Posts: 13

Original Poster
Rep: Reputation: 0
thanx for replies ...I will try it. i also found another suggestion whcih is to put bin/date instade of just date in file name.It will solve env variable problem.
 
Old 02-23-2007, 12:06 PM   #6
kashyapvirgo
LQ Newbie
 
Registered: Feb 2007
Posts: 13

Original Poster
Rep: Reputation: 0
back slash before % works completely fine thanx
 
Old 02-24-2007, 01:07 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
You're welcome! Just a little note about your last post! Is it true that issuing commands in crontab with their fullpath avoids some problems, since the crontab environment has a very limited PATH, usually /bin:/usr/bin. In the case of /bin/date this is totally not nedeed, because /bin is always in the PATH of cron. Not an error, anyway...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron job question a2vr6 Linux - Newbie 8 11-17-2006 01:33 PM
Simple Cron Job question FreezEy Slackware 3 07-11-2006 01:30 PM
Cron Job Question tarballed Linux - Newbie 4 06-20-2006 03:39 PM
Cron Job Question abhijeetudas Linux - Software 2 03-01-2006 11:27 PM
Quick Question about cron job andy18 Linux - General 2 08-11-2003 04:16 PM


All times are GMT -5. The time now is 06:25 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration