Does cron expression in unix/linux allow specifying exact start and end dates

14,185

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
14,185
vnkotak
Author by

vnkotak

Updated on June 22, 2022

Comments

  • vnkotak
    vnkotak almost 2 years

    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.
  • Jonathan Leffler
    Jonathan Leffler about 15 years
    I would have to disagree - though it depends slightly on whether you assume that the program will serve a purpose other than when run via cron (and Unix geeks do assume that it would). If it does serve a general purpose, then either building in an arcane set of rules to decide when it should be run or parsing a config file is serious overkill and not recommended (unless, perhaps, you already have tools in your library to do the checking). Much better to let 'cron' or a variant deal with that!
  • StackUnder
    StackUnder almost 11 years
    I was actually searching a way to make a cron job run until a due date, maybe creating a second job to remove both, until I red your words about " the script decide on its own whether or not it should do anything useful". Instead of creating a bunch of jobs, I'll just create one to call my script every day and it will decide if there's something to do.