Does Cron Expression in Unix/Linux Allow Specifying Exact Start and End Dates

Cron expression for particular date

Original question was tagged cron so this first section applies to that. See below for an updated answer for the Quartz CronTrigger tool.


Most crontabs don't let you specify the year so you'll probably have to put that in the script itself (or a wrapper around the script/program).

You can do this with something like:

# Only run in 2010.
if [[ $(date +%Y) != 2010 ]] ; then
exit
fi

The option you're looking for to run at 6am on September 6 every year is:

0 6 6 9 * your_command_goes_here
│ │ │ │ │
│ │ │ │ └─ any day of the week.
│ │ │ └─── 9th month (September).
│ │ └───── 6th day of the month.
│ └─────── 6th hour of the day.
└───────── Start of the hour (minutes = 0).

For the Quartz CronTrigger format, you'd be looking at something like:

0 0 6 6 9 ? 2010
│ │ │ │ │ │ │
│ │ │ │ │ │ └─ 2010 only.
│ │ │ │ │ └───── any day of the week.
│ │ │ │ └─────── 9th month (September).
│ │ │ └───────── 6th day of the month.
│ │ └─────────── 6th hour of the day.
│ └───────────── Start of the hour (minutes = 0).
└─────────────── Start of the minute (seconds = 0).

Run CRON job everyday at specific time

Cron utility is an effective way to schedule a routine background job at a specific time and/or day on an on-going basis.

Linux Crontab Format

MIN HOUR DOM MON DOW CMD

Sample Image

Example::Scheduling a Job For a Specific Time

The basic usage of cron is to execute a job in a specific time as shown below. This will execute the Full backup shell script (full-backup) on 10th June 08:30 AM.

Please note that the time field uses 24 hours format. So, for 8 AM use
8, and for 8 PM use 20.

30 08 10 06 * /home/yourname/full-backup
  • 30 – 30th Minute
  • 08 – 08 AM
  • 10 – 10th Day
  • 06 – 6th Month (June)
  • *– Every day of the week

In your case, for 2.30PM,

30 14 * * * YOURCMD
  1. 30 – 30th Minute
  2. 14 – 2PM
  3. *– Every day
  4. *– Every month
  5. *– Every day of the week

To know more about cron, visit this website.

Cron Job to run on a range of days only on a particular day of week

This is the crontab format:

* * * * *
| | | | |
| | | | +---- Day of the Week (range: 0-6, 0 standing for Sunday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month (range: 1-31)
| +---------- Hour (range: 0-23)
+------------ Minute (range: 0-59)

Ubuntu man 5 crontab says:

  field          allowed values
----- --------------
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

So, this should work for you:

0 0 2-31 * 0         /home/ubuntu/x.h
0 0 2-31 * 1-6 /home/ubuntu/y.h

I'm not sure why 7 would run on Saturday--is your system time accurate and in the right timezone?

Edit: Ah, yes, unfortunately you cannot specify both the day of the week and the day of the month. From man 5 crontab:

Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example, ``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).

So, the answer is:

0 0 2-31 * *       test $(date +\%u) -eq 7 && /home/ubuntu/x.h
0 0 2-31 * * test $(date +\%u) -ne 7 && /home/ubuntu/y.h

$(date '+%u') returns 1-7 representing Monday thru Sunday. Try echo $(date '+%u') for an example.

How do Cron Steps Work?


Step values can be used in conjunction with ranges. Following a range
with "<number>" specifies skips of the number's value through the range. For
example, "0-23/2" can be used in the hours field to specify command
execution every other hour (the alternative in the V7 standard is
"0,2,4,6,8,10,12,14,16,18,20,22"). Steps are also permitted after an
asterisk, so if you want to say "every two hours", just use "*/2".

The "range" being referred to here is the range given before the /, which is a subrange of the range of times for the particular field. The first field specifies minutes within an hour, so */... specifies a range from 0 to 59. A first field of */55 specifies all minutes (within the range 0-55) that are multiples of 55 -- i.e., 0 and 55 minutes after each hour.

Similarly, 0-23/2 or */2 in the second (hours) field specifies all hours (within the range 0-23) that are multiples of 2.

If you specify a range starting other than at 0, the number (say N) after the / specifies every Nth minute/hour/etc starting at the lower bound of the range. For example, 3-23/7 in the second field means every 7th hour starting at 03:00 (03:00, 10:00, 17:00).

This works best when the interval you want happens to divide evenly into the next higher unit of time. For example, you can easily specify an event to occur every 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, or 30 minutes, or every 1, 2, 3, 4, 6, or 12 hours. (Thank the Babylonians for choosing time units with so many nice divisors.)

Unfortunately, cron has no concept of "every 55 minutes" within a time range longer than an hour.

If you want to run a job every 55 minutes (say, at 00:00, 00:55, 01:50, 02:45, etc.), you'll have to do it indirectly. One approach is to schedule a script to run every 5 minutes; the script then checks the current time, and does its work only once every 11 times it's called.

Or you can use multiple lines in your crontab file to run the same job at 00:00, 00:55, 01:50, etc. -- except that a day is not a multiple of 55 minutes. If you don't mind having a longer or shorter interval once a day, week, or month, you can write a program to generate a large crontab with as many entries as you need, all running the same command at a specified time.

Run Quartz Scheduler Job with specific start, end date and within time constraints

What is the error you get when you say it is not working?

You can try the following code (Edit: applies to Quartz 2.2). This approach does not specify the start/end dates and year in the cron expression, instead uses the Trigger methods to specify them.
(Note: I haven't tested it myself, let me know if it works for you)

Edit:
I had the chance to test this code, I ran the code below and kept changing the system clock and all triggers were successful between 9 am to 12 am from start to end date.

public class CronJob {

public static void main(String[] args) throws ParseException, SchedulerException {

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

JobDetail job = newJob(TestJob.class)
.withIdentity("cronJob", "testJob")
.build();

String startDateStr = "2013-09-27 00:00:00.0";
String endDateStr = "2013-09-31 00:00:00.0";

Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(startDateStr);
Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(endDateStr);

CronTrigger cronTrigger = newTrigger()
.withIdentity("trigger1", "testJob")
.startAt(startDate)
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 9-12 * * ?").withMisfireHandlingInstructionDoNothing())
.endAt(endDate)
.build();

scheduler.scheduleJob(job, cronTrigger);
scheduler.start();
}

public static class TestJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("this is a cron scheduled test job");
}
}
}

If the above code does not work, try to replace the cronSchedule("0 0 9-12 * * ?") with cronSchedule("0 0 9-12 ? * ?")

How do I set up cron to run a file just once at a specific time?

Try this out to execute a command on 30th March 2011 at midnight:

0 0 30 3 ? 2011  /command

WARNING: As noted in comments, the year column is not supported in standard/default implementations of cron. Please refer to TomOnTime answer below, for a proper way to run a script at a specific time in the future in standard implementations of cron.



Related Topics



Leave a reply



Submit