Trying to get yesterdays date using the date command in a cronjob or cron variable
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
Distribution: openSUSE(Leap and Tumbleweed) and a (not so) regularly changing third and fourth
Posts: 629
Rep:
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.
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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.