Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.
like image 285
Krishna Kumar Avatar asked Apr 01 '09 10:04

Krishna Kumar


People also ask

How does cron work in Linux?

Cron is a job scheduling utility present in Unix like systems. The crond daemon enables cron functionality and runs in background. The cron reads the crontab (cron tables) for running predefined scripts. By using a specific syntax, you can configure a cron job to schedule scripts or other commands to run automatically.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .

How do you specify the year in cron expression?

You could try running a cron job @yearly (at 00:00 on New Year's day) which looks at the current year using date(1) and updates the current crontab file to one appropriate for the new year.

How do you specify the time zone in cron expression?

Cron job uses the server's define timezone (UTC by default) which you can check by typing the date command in terminal. When you cd into this directory you will see the name of different countries and their timezone. Command to change server timezone.


1 Answers

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.

like image 97
paxdiablo Avatar answered Oct 18 '22 17:10

paxdiablo