Precise Cron Job Scheduling: How to Run Every 10 Minutes Between 4:40 AM and 12:15 AM


2 views

When dealing with cron jobs, most developers are familiar with basic time range syntax like 4-23,0 for running between 4 AM and midnight. However, this approach doesn't work when you need more precise scheduling boundaries that don't align with whole hours.

The specific requirement here is to run a job:

  • Every 10 minutes
  • Starting at 4:40 AM
  • Ending at 12:15 AM (next day)
  • Between these times only

The correct cron syntax would be:


40-59/10 4 * * * script
*/10 5-23 * * * script
0,10 0 * * * script

Let's break this down:

1. The 4 AM Hour (Starting at 4:40)


40-59/10 4 * * * script

This handles the 4 AM hour specifically, starting at 40 minutes and running every 10 minutes until 4:50 (the last execution in this hour).

2. The Full Hours (5 AM to 11 PM)


*/10 5-23 * * * script

This is the standard every-10-minutes pattern that runs through all complete hours between 5 AM and 11 PM.

3. The Midnight Hour (Up to 0:15)


0,10 0 * * * script

This handles the first two executions in the midnight hour (0:00 and 0:10), stopping before 0:15 as required.

For those who prefer a single-line solution, you could use:


40-59/10 4 * * * script
*/10 5-23,0 * * * script

This combines the middle and last cases, but note it will run at 0:00, 0:10, and 0:20 (extending beyond 0:15). If the 0:20 execution is acceptable, this may be preferable for simplicity.

Let's verify the exact execution times:


1. 4:40
2. 4:50
3. 5:00
4. 5:10
...
N-2. 23:50
N-1. 0:00
N. 0:10

This gives us exactly the required schedule from 4:40 through 0:10 (the last execution before 0:15).

When implementing this schedule, consider:

  • Timezone settings on your server
  • Daylight saving time changes
  • Server reboots during the scheduled period
  • Job execution time overlapping with the next scheduled run

When scheduling cron jobs with specific time window constraints, we often encounter scenarios where the standard hour-based ranges (e.g., 4-23) don't precisely match our operational requirements. The case here demands execution between 4:40 and 0:15 (12:15 AM) with 10-minute intervals - a combination that requires special handling in cron syntax.

The initial attempt:

*/10   4-23,0  *   *   * script

falls short because:

  • It starts at 4:00 instead of 4:40
  • It includes all minutes from 4:00 to 4:50 when we only want 4:40
  • The 0 hour includes all minutes from 0:00 when we only need up to 0:15

We need to break this down into several cron expressions:

40,50     4     * * * script
*/10      5-23  * * * script
0,10      0     * * * script

For systems supporting more advanced cron syntax (like recent versions of cronie or Vixie cron):

40-59/10  4     * * * script
*/10      5-23  * * * script
0-15/10   0     * * * script

Let's validate this would trigger at:

4:40
4:50
5:00
5:10
...
23:50
0:00
0:10

Note the absence of 4:30 (too early) and 0:20 (too late) which matches our requirements perfectly.

When implementing this:

  • Test thoroughly across DST transitions
  • Consider adding timezone specifications (TZ= variable)
  • For complex scheduling, evaluate alternative tools like systemd timers

Here's how this would look in an actual crontab:

# Run every 10 mins between 4:40 and 0:15
40,50     4     * * * /usr/bin/env bash /path/to/script.sh
*/10      5-23  * * * /usr/bin/env bash /path/to/script.sh
0,10      0     * * * /usr/bin/env bash /path/to/script.sh