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

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


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    exitfi

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


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


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.