LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 07-22-2014, 07:39 AM   #1
zWaR
Member
 
Registered: Dec 2003
Distribution: Slackware, Alpine Linux, Ubuntu, Debian
Posts: 219

Rep: Reputation: 35
cron expression between 07/31/2014 and 08/07/2014


Hi all!

I'd like to construct a single cron expression which will run between July 31 and August 7 at 6:45 every day.

The following construct is not valid, because it will execute the code on August 31 as well:
Code:
45 6 31,1-7 7,8 * /myScript.sh
The problem can be solved with two cron expressions (one for July and the other for August), but can it be solved with only one expression?

Thanks for your input!
 
Old 07-22-2014, 09:39 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
cron can't do that directly. You would need to include a date test in the command string:
Code:
45 6 31,1-7 7,8 * [ $(date +\%j) -lt 220 ] && /myScript.sh
Just be sure to take it out of your crontab before next July, or it will run on July 1-7, 2015 and the other dates for that year as well. (Your subject did specify 2014.)

Last edited by rknichols; 07-22-2014 at 10:53 AM. Reason: Add missing backslash
 
1 members found this post helpful.
Old 07-22-2014, 10:16 AM   #3
zWaR
Member
 
Registered: Dec 2003
Distribution: Slackware, Alpine Linux, Ubuntu, Debian
Posts: 219

Original Poster
Rep: Reputation: 35
Thanks rknichols! Good idea!

Re year, yeah sure. To make it generic one can do the following, right?
Code:
45 6 31,1-7 7,8 * [ $(date +%j) -gt 211 ] && [ $(date +%j) -lt 220 ] && /myScript.sh
 
Old 07-22-2014, 10:53 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Note that I neglected to escape the "%" sign that is special in a crontab. Sorry about that. Edited to fix.

You would need to change that "220" to "221" to work right in leap years too. And, since date is a fairly complex command, you might just call it once:
Code:
45 6 31,1-7 7,8 * D=$(date +\%j); [ $D -gt 211 ] && [ $D -lt 221 ] && /myScript.sh
It looked like this was just a temporary thing since you mentioned "2014", so I didn't try to be rigorous.
 
1 members found this post helpful.
Old 07-22-2014, 11:02 AM   #5
zWaR
Member
 
Registered: Dec 2003
Distribution: Slackware, Alpine Linux, Ubuntu, Debian
Posts: 219

Original Poster
Rep: Reputation: 35
Thanks rknichols, that's really helpful!
 
  


Reply

Tags
cron cronjob month



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
LXer: Flock 2014 is on the way how about you? LXer Syndicated Linux News 0 07-16-2014 02:11 AM
1984 has become reality in 2014 jlinkels General 13 06-05-2014 12:43 PM
LXer: Best Linux distributions for 2014? LXer Syndicated Linux News 0 01-15-2014 10:10 PM
LXer: FOSDEM 2014 Brussels / 1 & 2 February 2014 LXer Syndicated Linux News 0 11-29-2013 05:10 PM
LXer: HTML5 kicked into 2014 LXer Syndicated Linux News 0 02-15-2011 12:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:18 PM.

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