Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a cron job every minute only on specific hours?

Tags:

How do you run a cron job every minute only on specific hours? Like so:

It will only check every minute from 11AM to 12AM, 4PM to 5PM and 9PM to 10PM

It seems to be complicated to me I don't know where to start.

like image 253
Jürgen Paul Avatar asked Aug 11 '12 11:08

Jürgen Paul


People also ask

How do I run a Cron job after every minute?

Execute Cron Job After System Reboot. Setup and Run PHP Script As A Cron Job. Run crontab job every minute on a Linux or Unix-like system.

How do I run a Cron job every 3 hours?

You can set the cron for every three hours as: 0 */3 * * * your command here ..

How do I schedule a script in crontab to run every 5 minutes?

basic 3. /usr/bin/vim. tiny 4. /bin/ed Choose 1-4 [1]: Make a new line at the bottom of this file and insert the following code. Of course, replace our example script with the command or script you wish to execute, but keep the */5 * * * * part as that is what tells cron to execute our job every 5 minutes.


2 Answers

Right solution:

* 11,16,21 * * * 

Because if you use previous solution:

0-59 11-12,16-17,21-22 * * * * 

Job will start at 12:40 or 17:59. It is not in range from 11AM to 12AM, 4PM to 5PM and 9PM to 10PM.

UPDATE:

Traditional (inherited from Unix) cron format consists of five fields separated by white spaces:

*    *    *    *    *  command to be executed ┬    ┬    ┬    ┬    ┬ │    │    │    │    │ │    │    │    │    │ │    │    │    │    └───── day of week (0 - 6) (0 is Sunday, or use names) │    │    │    └────────── month (1 - 12) │    │    └─────────────── day of month (1 - 31) │    └──────────────────── hour (0 - 23) └───────────────────────── min (0 - 59) 

nnCron can use both traditional and "enhanced" version of cron format, which has an additional (6th) field: Year.

like image 102
Vadim Baryshev Avatar answered Sep 21 '22 22:09

Vadim Baryshev


As per the cron format

<Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week> <Year>  * * * * * * | | | | | |  | | | | | +-- Year              (range: 1900-3000) | | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday) | | | +------ Month of the Year (range: 1-12) | | +-------- Day of the Month  (range: 1-31) | +---------- Hour              (range: 0-23) +------------ Minute            (range: 0-59) 

the solution should be

* 11,16,21 * * * * 
like image 36
Bharat Sinha Avatar answered Sep 22 '22 22:09

Bharat Sinha