LinuxQuestions.org
Visit Jeremy's Blog.
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-24-2018, 12:04 PM   #1
malbe
LQ Newbie
 
Registered: May 2018
Posts: 3

Rep: Reputation: Disabled
Trying to get yesterdays date using the date command in a cronjob or cron variable


I am trying to add an argument with yesterdays date in a cronjob or make the date a variable in the crontab
I have tried several ways. I can get it to work with using todays date: `date +\%Y\%m\%d` but not yesterdays.

Yesterdays Date
* * * * * . $HOME/.bashrc; /usr/copy-dissem.py amsr2_nh `date --date="yesterday" +\%Y\%m\%d` >> /usr/amsr2_nh-dissem.log 2>&1
 
Old 05-24-2018, 12:12 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Does "yesterday" alone work in the cron job?
That format string and set of arguments in the normal shell seem to work fine.
 
Old 05-24-2018, 12:16 PM   #3
malbe
LQ Newbie
 
Registered: May 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Are you asking if just typing * * * * * . $HOME/.bashrc; /usr/copy-dissem.py amsr2_nh yesterday >> works? If so no it doesn't work.
 
Old 05-24-2018, 12:22 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
No I was asking if you tried using the date command with --date="yesterday" only and did not include the format string. I was asking whether or not that worked.

* * * * * . $HOME/.bashrc; /usr/copy-dissem.py amsr2_nh `date --date="yesterday"` >> /usr/amsr2_nh-dissem.log 2>&1
 
Old 05-24-2018, 12:27 PM   #5
malbe
LQ Newbie
 
Registered: May 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Ok I tried it no luck. I think the problem area is the syntax of the quotation marks. The quotation marks show up red and give an error when I am creating the cronjob. I have tried other ways but they did not work.

`date --date=(yesterday) +\%Y\%m\%d`
`date --date='yesterday' +\%Y\%m\%d`
 
Old 05-24-2018, 12:54 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,885
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
How about no quotation marks?

Also try removing the back slashes from the format string term.

But I'd try without the format string term to verify that the keyword yesterday works properly.

Along with "yesterday" you can instead do "-1 day" or '-1 day', but NOTE that those are forward single quotes.
 
Old 05-24-2018, 01:29 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,813

Rep: Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958
Since % is a cron special character they need to escaped if used in the command.
You might try replacing the back ticks with $() but I don't think it matters.

The command looks like it should work but try the others and see if what happens.
 
1 members found this post helpful.
Old 05-24-2018, 02:01 PM   #8
petelq
Member
 
Registered: Aug 2008
Location: Yorkshire
Distribution: openSUSE(Leap and Tumbleweed) and a (not so) regularly changing third and fourth
Posts: 629

Rep: Reputation: Disabled
How about writing your command in a bash script and just using crontab to run the script.
I've found crontab a little choosy as to what instructions to follow.
 
2 members found this post helpful.
Old 05-24-2018, 04:16 PM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by petelq View Post
How about writing your command in a bash script and just using crontab to run the script.
I've found crontab a little choosy as to what instructions to follow.
Yes, when crontab lines tend to be long it's better to write commands in a script and let cron call this script. Also, it's easier to add variables if needed

Last edited by keefaz; 05-24-2018 at 04:17 PM.
 
Old 05-24-2018, 05:49 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,813

Rep: Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958Reputation: 5958
Here is the first attempt to duplicate the OPs cron problem. The basic cron job works with yesterday so something maybe different about their environment.

Code:
#!/bin/bash
echo $1 $2 >> /home/username/data.txt
Code:
* * * * * /home/username/my_program test1 $(date -d "yesterday" +\%Y\%m\%d\%M)
and

* * * * * /home/username/my_program test1 `date -d "yesterday" +\%Y\%m\%d\%M`
Code:
data.txt

test1 2018052325
test1 2018052326
test1 2018052327
test1 2018052328
test1 2018052329
test1 2018052330
test1 2018052331

test1 2018052342
test1 2018052343
test1 2018052344

Last edited by michaelk; 05-24-2018 at 05:50 PM.
 
Old 05-24-2018, 07:39 PM   #11
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 10,596

Rep: Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503
I'm not really sure what you are trying to accomplish here but if you want output in YYYY MM DD format, output to a file, the command below worked for me in cron.

Code:
08 * * * * date --date "yesterday" > /home/user/data.txt
The date output to data.txt was:

Quote:
Wed May 23 17:12:01 MST 2018
Other options in cron such as below didn't output anything to the data.txt file although the command output the expected "2018\05\23" from a terminal and also worked in a script and output the date in format: 2018\05\23.

Quote:
date -d "yesterday" "+%Y\%m\%d" /home/user/data.txt

Last edited by yancek; 05-24-2018 at 08:05 PM.
 
  


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
System date and time + timezone with DATE command wBB Linux - Newbie 10 07-29-2017 09:27 AM
date:- date command giving different results. rajivswe.2k7 Linux - Newbie 6 08-10-2016 08:29 AM
NEWBIE: grep using yesterdays date? hristo77 Programming 1 11-05-2015 06:42 AM
date variable user52 Linux - Newbie 2 11-28-2006 08:48 AM
using date in cron cbriscoejr Linux - Distributions 5 01-06-2005 07:26 AM

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

All times are GMT -5. The time now is 03:30 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