Set up Cronjob to run every 5 minutes, when it’s between 5 to 20 hours

Asked

Viewed 29,792 times

22

I need to make a configuration so that mine cronjob function every 5 minutes. However, if the time of day is before 5and greater than 20 I don’t want you to spin.

I mean, I want a make one cron running every 5 minutes, but only in the range of 5 to 20 hours.

How can I do that?

Currently, I have a cron that so:

 0,30 * * * * php ~/pasta/para/app/artisan queue:work

What change should I make to make it work the way I want it to?

  • From what I see seems to be an application of Laravel, It would not be easier to use a Schedule and call the command ?

  • Laravel was just an example. The tags and the content of the question were in order to understand about cronjob itself

1 answer

38


The */5 means "any minute, but every five".

Depending on the implementation, that’s enough

 */5 5-20 * * * php ~/pasta/para/app/artisan queue:work


Syntax of crontab:

*   *   *   *   *       caminho/comando
│   │   │   │   │
│   │   │   │   └────── em quais dias da semana de 0 a 7 (tanto 0 quanto 7 são Domingo)
│   │   │   └────────── em quais meses    (1 - 12)
│   │   └────────────── em quais dias     (1 - 31)
│   └────────────────── em quais horas    (0 - 23)
└────────────────────── em quais minutos  (0 - 59)

Specifying each item:

*        Todos
1,2,4    Um, dois e Quatro apenas
7-10     De 7 a 10, incluindo 8 e 9
*/5      A qualquer momento, mas com espaço de 5 (ex: 2,7,12,17...)
1-10/3   No intervalo de 1 a 10, mas de 3 em 3 (ex: 2,5,8)

Note: when you use /n, depends on when the cron is updated by the table that the range is counted, therefore, */5 can be either 0,5,10,15 or 1,6,11,16.

An example if it needed to be every 3 hours in that interval:

 */5 5-20/3 * * * php ~/pasta/para/app/artisan queue:work

Remark: the cron there are no intervals in some implementations (I don’t know if this proceeds in any modern distro yet), you may need to specify all hours:

*/5 5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 * * * php ~/pasta/para/app/artisan queue:work

More details in the crontab:

http://linux.die.net/man/5/crontab

Browser other questions tagged

You are not signed in. Login or sign up in order to post.