17
votes

I want to be able to configure something like this.

  1. I want to run job 'X' at 7 AM everyday starting from 29/june/2009 till 30/12/2009. Consider current date as 4/4/2009.
5

5 Answers

19
votes

It can be done in a tricky sort of way.

You need three separate cron jobs for that range, all running the same code (X in this case):

  • one for the 29th and 30th of June ("0 7 29,30 6 * X").
  • one for every day in the months July through November ("0 7 * 7-11 * X").
  • one for all but the last day in December ("0 7 1-30 12 * X").

This gives you:

# Min   Hr   DayOfMonth   Month   DayOfWeek   Command
# ---   --   ----------   -----   ---------   -------
   0     7      29,30        6        *          X
   0     7          *     7-11        *          X
   0     7       1-30       12        *          X

Then make sure you comment them out before June 29, 2010 comes around. You can add a final cron job on December 31 to email you that it needs to be disabled.

Or you could modify X to exit immediately if the year isn't 2009.

if [[ "$(date +%Y)" != "2009" ]] ; then
    exit
fi

Then it won't matter if you forget to disable the jobs.

6
votes

Yes, mostly. Some cron implementations have support for years, some don't, so we'll assume yours does not. Also, I'm making the assumption that this job is only being run by the cron daemon, so we can use the execute bit to determine whether or not cron should run the job.

Note that you'll need to leave your script as non-executable until such time as you want it to run.

The following cron expressions will do what you want (every day, including weekends). Tweak as you need to:

# Make the job executable on 29 June.
0 6 29 6 * chmod +x /path/to/my/job/script

# Run the job between June and December, only if it's executable.
0 7 * 6-12 * test -x /path/to/my/job/script && /path/to/my/job/script

# Disable execution after 30 December.
0 8 30 12 * chmod -x /path/to/my/job/script
2
votes

I'm usually a fan of keeping the logic with the program being run. You might think about setting up one cron job that runs the script every day, then have the script decide on its own whether or not it should do anything useful. When the last useful day (Dec 30) has passed, your script could remove itself from the crontab. In the script you can set up the logic with all the comments necessary to describe what you are doing and why.

If your job is a binary program, you might set up a run_script that does this schedule filtering work before calling the program.

-1
votes

You can use this to generate a crontab that runs at specific intervals:

http://www.robertplank.com/cron/

Or this

http://www.webmaster-toolkit.com/cron-generator.shtml

One solution would be to setup 6 crons, 1 for each month, each would run at 7 am every day that month.

It's probably the easiest way, the next one up would be to script it.

-1
votes

No, afaik, you cannot do that.

The cron fields hold the values for minutes, hours, day of month, month and day of week, respectively.

10 5 10 * * means run at 5:10 on every 10th of every month.

10 5 * 12 * means run at 5:10 on every day in december

10 5 * * 1  means run at 5:10 every Monday

You can make it run on a series of specific months, as the crontab format does accept ranges. April through December would be 4-12 in that case for the month field. But that does not take into account your wish for having this limited to 2009.

There is no mechanism to set start and stop dates for cronjob. You can always script this of course. Make a cronjob run every day and check the current date to be before 30/12. If it is 31/12 make it remove itself. Or something more thought through.

A crontab of

0 7 * * 6-12 command_X

would do what you want partially, but it would start at June 1st and run through December 31st. Skipping the first part of June and December 31st would have to be scripted in the X command.