Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I write a cron that will exclude a specific date.

let say : I need to run a crontab 6pm to 9pm in every 30 min on weekends and want to exclude 10th may 2014 only.

Is It possible? How can I write this? or is any alternative for this? please suggest.

share|improve this question
2  
homework problem? –  mdpc May 9 at 8:12
    
@mdpc : I have searched a lot and didn't get proper solution for it. I know about the cron but don't know how to exclude a particular date. If you have any idea please share. –  RKP May 9 at 9:12

2 Answers 2

Standard cron syntax is quite simple, it does not support exclusions. In some cases it is possible to create a list of several/many cron entries to implement such logic, but that tends be tedious and hard to understand or maintain; this approach is not applicable in your case though (not least because standard cron has no notion of the calendar year).

It's not a great plan to make cron commands complicated, but you can do this with something relatively straightforward like:

0,30 18-20 * * sat,sun [ "$(date +%Y%m%d)" = "20140510" ] || mycommand
0    21    * * sat,sun [ "$(date +%Y%m%d)" = "20140510" ] || mycommand

(With "0,30 18-21" the task would also run at 21:30; you need the second line if you wish to run also at 21:00, if not then omit it)

The cron command is really a shell command list, the || control operator provides short-circuit evaluation of commands. Commands in the list are attempted one at a time, from left to right, but the execution stops when first one returns true. If the date string comparison returns true (only true for one day) mycommand is not run, when the date comparison returns false mycommand is run.

Note however, many things can differ in the way cron starts programs. The most robust approach is to use a wrapper script to implement your conditional logic.

share|improve this answer

You can use X-Y for time intervals and */Z for frequencies. Where:

  • X is the start of the interval.
  • Y is the end of the interval.
  • Z is just a divisor (i.e.: each Z).

For excluding a date I would just create my own script. Within this script, check date and then decide if you want to execute the following instruction(s).

share|improve this answer
    
The same way I am assuming I need to create a own script to exclude a specific date..Can't do using cron. Thanks. –  RKP May 9 at 10:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.