Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run immediately then every 4 hours in cron

Tags:

cron

How do you write a cron job which immediately run, then run on every hour divisible by 4? Say I started the script at 13:25, the job fires up right away, then the next run would be at 16:00, 20:00, 00:00 and so on.

like image 452
oikonomiyaki Avatar asked Sep 18 '25 20:09

oikonomiyaki


2 Answers

For the first immediate run, just execute the command manually. Then set your cron up like this to have it execute continuously every 4th hour

 0 */4 * * * yourCommand

This will run yourCommand every 4 hours (00:00, 04:00, 08:00......)

like image 139
KeithC Avatar answered Sep 23 '25 10:09

KeithC


If you use it in pair with some prog language you can do as follows, (e.g. golang):

_, minutes, _ := time.Now().Clock()
fmt.Sprintf("%d * * * *", minutes+1)
like image 36
Fedor Avatar answered Sep 23 '25 11:09

Fedor