Shell script to generate daily crontab entries between to specific dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to generate daily crontab entries between to specific dates
# 1  
Old 11-27-2009
Shell script to generate daily crontab entries between to specific dates

Hi,

I am currently writing a shell script to enter daily reoccurring jobs into the crontab. I want to be able to specify any two dates for which I want these daily jobs to run between.

Below is an example of what I need to be written to crontab.

# Give a start day of 21/10/2009 09:00 and end day of 23/10/2009 09:00, we need the following crontab entries:

# 00 09 22 10 0-7 script.sh 10/21/2009 10/22/2009
# 00 09 23 10 0-7 script.sh 10/22/2009 10/23/2009

Currently, I am assuming that the two date span within a month, but I want to extend this so that the dates can span across any time frame.

How would I go about running through all the days between 2 particular dates?

Any help would be much appreciated.

Thanks,

Jess
# 2  
Old 11-27-2009
For example, you need run the script between 11/26/2009 and 11/28/2009 at 9 o'clock.

Code:
0 9 * * * script.sh 11/26/2009 11/28/2009

You can adjust your cronjob with different dates.

You need add some if-then-fi in your script.sh (code is not tested)

Code:
if [ "$2" = "" ]; then
  echo " please provide dates: $0 Date1 Date2"
  exit
fi

D1=`gdate -d "$1" +%s`
D2=`gdate -d "$2" +%s`
NOW=`gdate +%s`

if [ "${D1}" > "${D2}"] ; then
  echo "Date1 must older than Date2"
  exit
fi

if [ "$NOW" > "${D1}" && "$NOW" < "${D2}" ]; then

  # Put your script here.

fi

# 3  
Old 11-27-2009
Quote:
Originally Posted by JM_86
...
How would I go about running through all the days between 2 particular dates?
...
You can generally specify ranges and lists for the 3rd field i.e. day of the month. Check your man page for confirmation though.

So, for this case -

Code:
# Give a start day of 21/10/2009 09:00 and end day of 23/10/2009 09:00, we need the following crontab entries:

# 00 09 22 10 0-7 script.sh 10/21/2009 10/22/2009
# 00 09 23 10 0-7 script.sh 10/22/2009 10/23/2009

the following crontab entry is sufficient:

Code:
0 9 22,23 10 * script.sh

I have assumed that the input date parametes were passed only for the date-check criterion. If they are actually used in your script for some other purpose, then you'll have to pass them.

Quote:
...Currently, I am assuming that the two date span within a month, ...
Crontab entries for this case are simple. As an example, if you want to run a script at 9 AM on the 3rd, 5th, 15th, and 28th day and all days from 18th through 23rd of October, then you can mix lists (d1, d2, d3) and ranges (d1-d2, d3-d4) thusly:

Code:
0 9 3,5,15,18-23,28 10 * script.sh

A "*" for "day of week" (5th field) means that the day of week is disregarded.

Quote:
...but I want to extend this so that the dates can span across any time frame...
Things are not as simple for this case. In general, you can do this:

Code:
* * * * * <date_check_script> && script.sh

The <date_check_script> is a shell script that returns 0 if today lies within the intended date-range (the date-range might span a year or two, for example).

If the script is to be run at 9 AM every day from, say, 1-Nov-2009 through 30-Jun-2010, then you could have this:

Code:
0 9 * 1-6,11-12 * <date_check_script> && script.sh

and your <date_check_script> should return 0 if
(a) current month is [11,12] and year is 2009, or
(b) current month is [1-6] and year is 2010.

So you could have something like this:

Code:
$ 
$ # Note that the input parameter, the "-d" option of date and the printf commands are just for testing
$ # You can simply work with yr=$(date +%Y) and mon=$(date +%m)
$ # to consider date as "now"
$ cat -n date_check.sh       
     1  #!/bin/bash          
     2  YFMT=$1              
     3  yr=$(date -d "$YFMT" +%Y)
     4  mon=$(date -d "$YFMT" +%m)
     5  printf "year = $yr\tmon  = $mon\t"
     6  if [ "$mon" -ge "11" ] && [ "$yr" -eq "2009" ]; then
     7    # echo "mon ge 11 and yr eq 2009 ; return 0"      
     8    return 0                                          
     9  elif [ "$mon" -le "06" ] && [ "$yr" -eq "2010" ]; then
    10    # echo "mon le 06 and yr eq 2010 ; return 0"        
    11    return 0                                            
    12  else                                                  
    13    # echo "Outside range [1-Nov-2009, 30-Jun-2010] ; return 1"
    14    return 1                                                   
    15  fi                                                           
    16                                                               
$                                                                    
$ # now test the date_check.sh script
$ for i in jan feb mar apr may jun jul aug sep oct nov dec; do
>   . date_check.sh "1 $i 2009" ; echo "Return Value = $?"
> done
year = 2009     mon  = 01       Return Value = 1
year = 2009     mon  = 02       Return Value = 1
year = 2009     mon  = 03       Return Value = 1
year = 2009     mon  = 04       Return Value = 1
year = 2009     mon  = 05       Return Value = 1
year = 2009     mon  = 06       Return Value = 1
year = 2009     mon  = 07       Return Value = 1
year = 2009     mon  = 08       Return Value = 1
year = 2009     mon  = 09       Return Value = 1
year = 2009     mon  = 10       Return Value = 1
year = 2009     mon  = 11       Return Value = 0
year = 2009     mon  = 12       Return Value = 0
$
$ # and for year 2010
$ for i in jan feb mar apr may jun jul aug sep oct nov dec; do
>   . date_check.sh "1 $i 2010" ; echo "Return Value = $?"
> done
year = 2010     mon  = 01       Return Value = 0
year = 2010     mon  = 02       Return Value = 0
year = 2010     mon  = 03       Return Value = 0
year = 2010     mon  = 04       Return Value = 0
year = 2010     mon  = 05       Return Value = 0
year = 2010     mon  = 06       Return Value = 0
year = 2010     mon  = 07       Return Value = 1
year = 2010     mon  = 08       Return Value = 1
year = 2010     mon  = 09       Return Value = 1
year = 2010     mon  = 10       Return Value = 1
year = 2010     mon  = 11       Return Value = 1
year = 2010     mon  = 12       Return Value = 1
$
$

HTH,
tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script newbie- how to generate service log from shell script

Hi, I am totally a newbie to any programming languages and I just started an entry level job in an IT company. One of my recent tasks is to create a script that is able to show the log file of linux service (i.e. ntpd service) lets say, if I run my script ./test.sh, the output should be... (3 Replies)
Discussion started by: xiaogeji
3 Replies

2. Shell Programming and Scripting

Generate documentation for a shell script

Hi, I've written a shell script with proper intentation and commenting structure. However, I would like to generate documentation for the shell which I have written. Is there any tool as such to generate it like we have javagen/docgen ? Please help. Thanks, Arjun (0 Replies)
Discussion started by: arjun_arippa
0 Replies

3. Shell Programming and Scripting

Running script in crontab in a specific directory

I am trying to run a script from crontab but the entire script (which is 70+ lines) is written in bash and I need it to run from a certain directory. So when I run it manually I cd to /local/mnt/scr and then type ./reordersc and it works fine. However, I want it to run from the crontab and I... (20 Replies)
Discussion started by: newbie2010
20 Replies

4. Shell Programming and Scripting

Need simple script to generate future dates

Hi All, I am looking into a script which will give me a future dates for 7 days, including next month dates in case if runs on month ends... I an able to get this in Linux but not working for Solaris. OS: Solaris 10 Please help (6 Replies)
Discussion started by: nanz143
6 Replies

5. Shell Programming and Scripting

generate logfile in a shell script

Unix Gurus, I have a shell script which has few "echo" statements. I am trying to create a logfile where all the outputs of the echo statement sare stored. I will have to add this as the final step in the existing script so that everytime the script runs, a logfile is generated with all the... (1 Reply)
Discussion started by: shankar1dada
1 Replies

6. Shell Programming and Scripting

Script to search specific folders dates /mm/dd/ structure

Hi, I have a script that handles a huge amount of log files from many machines and copies it into a SAN location with the following directory structure: /SAN/machinenames/yyyy/m/d so for example /SAN/hosta/2011/3/12/files* Now I am writing a bash script to search for files between to date... (4 Replies)
Discussion started by: GermanJulian
4 Replies

7. UNIX for Dummies Questions & Answers

Generate dates

let says, today is my payroll day (7/26), my next payroll day will be 8/9. i want to generate a shell script to extract 8/9, 8/23, 9/6, and so on for 2010. (2 Replies)
Discussion started by: tjmannonline
2 Replies

8. Shell Programming and Scripting

output file of the shell script running through crontab is deleting automatical daily.

Dear Friends, I am working on IBM AIX. I have written one script and kept in the crontab as to run daily at 11:38 AM. and the output of the script to be appended to the file generated with the month name. but my file deleting daily and the new file is creating with the output of the shell... (2 Replies)
Discussion started by: innamuri.ravi
2 Replies

9. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

10. UNIX for Dummies Questions & Answers

generate xml from a shell script

Hello! I would like to generate an xml file from the output of various commands generated from within a shell script (some will be in CDATA). At the moment the only solution I have come up with is echoing xml tags around the commands eg. echo "<bitism>" >> outputfile /usr/sbin/prtconf... (1 Reply)
Discussion started by: speedieB
1 Replies
Login or Register to Ask a Question