LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 01-06-2006, 12:49 PM   #1
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Rep: Reputation: 16
Using `date '+%Y%m%d'` in crontab?


Hey everyone,

I'm trying to figure out how to use cron to generate a file containing today's date, such as 'ls.log.20060105'.

I know Cron uses /bin/sh .

If I switch to /bin/sh , the command "ls foo bar > ls.log.`date +%Y%m%d` 2>&1" creates the file correctly. However, the file is NOT created when I put this same command in my crontab.

I've tried the following methods, and none of them create the file 'ls.log.20060105':

Code:
0 10 * * * ls foo bar > ls.log.`date +%Y%m%d` 2>&1

# Use a variable
0 10 * * * DATE=`date +%Y%m%d` ; ls foo bar > ls.log.$DATE 2>&1

# Use single quotes
0 10 * * * DATE=`date '+%Y%m%d'` ; ls foo bar > ls.log.$DATE 2>&1

# Escape the % characters with a \
0 10 * * * DATE=`date +\%Y\%m\%d` ; ls foo bar > ls.log.$DATE 2>&1

# Use single quotes AND escape the % characters with a \
0 10 * * * DATE=`date '+\%Y\%m\%d'` ; ls foo bar > ls.log.$DATE 2>&1
I appreciate any help,

Thank you,
-= Stefan
 
Old 01-06-2006, 01:13 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,973
Blog Entries: 5

Rep: Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778
Try putting in the full path of the date command instead of just date. (e.g. /bin/date).
 
Old 01-06-2006, 01:56 PM   #3
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Also, I don't think variable expansion works well (if at all) in cronjobs. Rather than using $DATE, try (as you did in your test) just putting the expression there with the backticks.
 
Old 01-06-2006, 02:03 PM   #4
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Original Poster
Rep: Reputation: 16
Excellent. Thanks for the help. Cron drives me crazy.

It works when I use the full path to 'date', no variable expansion (although that does work on some OSes), and escaping with backslashes works fine.

Code:
0 11 * * * ls foo bar > ls.log.`/usr/bin/date +\%Y\%m\%d` 2>&1
Thanks again!
 
Old 01-06-2006, 02:06 PM   #5
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Cron can be a beast sometimes. Most often when I need to do something of any complexity, I write a (small) script to do it and have cron just call that script for cleanliness.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Start Date + Time Duration = End Date/Time calculator? ToBe Linux - General 3 09-26-2005 10:17 AM
crontab ashley75 Linux - General 3 07-21-2005 10:48 AM
system-wide crontab in /etc/crontab ner Linux - General 2 11-18-2003 12:35 PM
Crontab Gorbachov Linux - Software 3 09-26-2003 04:40 PM
lastlog date does not match system date? jcmj Linux - Networking 6 10-22-2002 12:09 PM


All times are GMT -5. The time now is 01:33 AM.

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