LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-09-2009, 09:27 PM   #1
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Rep: Reputation: 30
run crontab exclude specific date


I would like to ask if I can run a crontab job to skip a specific date , eg. to skip the crontab job to run on holiday ? thx
 
Old 08-09-2009, 10:22 PM   #2
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
You could have a file (e.g. /somewhere/noscript.txt) listing the dates on which you don't want to run the script. e.g.
Code:
2009-10-10
2009-11-02
...
Then, the script called by cron first checks to see if today's date is in noscript.txt, and, if so, exits immediately:
Code:
#!/bin/sh

if [ grep -q `date +%F` /somewhere/noscript.txt ]; then
  exit 0
fi

...
 
Old 08-09-2009, 10:22 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
I would like to ask if I can run a crontab job to skip a specific date , eg. to skip the crontab job to run on holiday ? thx
Not really. You're best of to write a wrapper script to whatever
you want to run. Have cron attempt to run it every day, and inside the
wrapper-script check for the date you *don't* want to run your job
and exit if it's the current date.

E.g., something like this:
Code:
#!/bin/bash
TODAY=$(date "+%Y%m%d")
if [ "$TODAY" -ne "20091224" ]; then
  run your job
else
  exit 0
fi

Cheers,
Tink

Last edited by Tinkster; 08-09-2009 at 10:25 PM. Reason: beat by a few seconds =D
 
Old 08-10-2009, 12:36 AM   #4
elainelaw
Member
 
Registered: Jan 2008
Posts: 258

Original Poster
Rep: Reputation: 30
thx all much.
 
Old 08-10-2009, 06:35 AM   #5
jeromeNP7
Member
 
Registered: Jun 2009
Posts: 101

Rep: Reputation: 19
Holiday dates are different globally, so it wouldn't make sense to make cron more complicated just to take care of that. It would be very error-prone, I guess. Beyond the already suggested wrapper scripts you can also add a single line script directly into the crontab file to run a test condition before launching the real task. A sample crontab entry:
0 0 * * * if [ $(date "+%Y%m%d") -ne "20091224" ]; then echo Non-Holiday; else echo Holiday; fi;

Linux

Last edited by jeromeNP7; 09-04-2009 at 09:21 PM.
 
Old 12-30-2009, 11:53 AM   #6
JurgyMan
Member
 
Registered: Jan 2004
Location: USA
Distribution: Gentoo
Posts: 60

Rep: Reputation: 15
isholiday

had same exact problem... wrote a script others could use too:

laptop: cat isholiday
#!/bin/ksh
# Written: 12-30-09 By: AWJurgensen
# Intent: Check if today or supplied date is a holiday
# Usage: isholiday [YYYYMMDD]
# Files: ~/.holidays contains Declared Holidays in YYYYMMDD fmt
# Tip: Useful in crontab to exclude jobs on holidays:
# * * * * * bin/isholiday || bin/runmyjob

HOL=$(echo ~/.holidays)
TODAY=$(date "+%Y%m%d")
DATE=${1:-${TODAY}}

if [ ! -s $HOL ] ; then
echo "$0: Error: $HOL file not found!"
elif [ "$(grep "^$DATE" $HOL)" != "" ] ; then
exit 0
else
exit 1
fi

laptop: cat ~/.holidays
20100101 New years Day
20100118 Martin Luther King Day
20100531 Memorial Day
20100705 Independence day
20100906 Labor Day
20101111 Veterans Day
20101125 Thanksgiving Day
20101225 Christmas
laptop: isholiday 20100101 && echo ishol
ishol
laptop: isholiday 20100102 && echo ishol
laptop:
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Using `date '+%Y%m%d'` in crontab? stefanlasiewski Linux - Software 6 07-01-2015 02:25 PM
IsThere a fridge, that we can program at specific date/time to unfreeze specific food frenchn00b General 3 07-21-2009 11:26 PM
Exclude files from rm -rf based on date stefaandk Programming 3 01-23-2009 10:24 PM
Regex to exclude a specific phrase grob115 General 5 11-19-2008 12:10 PM
How to exclude specific directories from an rsync backup kaplan71 Linux - Software 2 05-16-2008 12:09 PM

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

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