LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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.
 
Old 06-29-2015, 01:32 AM   #6
thamarin
LQ Newbie
 
Registered: Jun 2015
Posts: 4

Rep: Reputation: Disabled
name not correct after backup

i use scrip sample

DB-`date +%Y-%m-%d`


DB it uppercase


The result has been db-2015-06-28

i don't understand

help me!
 
Old 07-01-2015, 02:25 PM   #7
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
You should probably open a new thread for your question.

Giving us just the DB-.... doesn't help. We'd need to see what the script is doing at least in the section where you have that and probably the entire script. There are tools in Linux to convert upper case to lower case but nothing does it automatically without you telling it to do so. Without seeing the whole script we can't tell what is doing this in your script.
 
  


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
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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

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