Need to run cron job after every 3 hours

214 Views Asked by At

I need to run cron job every 3 hours after the start of job. Currently I am using

0 */3 * * *

If I start job now according to my time at 2:45 PM this command will complete its first interval at 3:00 PM and next interval at 6 PM. I want to complete my first interval exactly after 3 hours like if i start job at 2:45 PM my interval should complete at 5:45 PM. How I can achieve that? Thanks

2

There are 2 best solutions below

1
Maxim Mazurok On BEST ANSWER

Cron doesn't know when you "start running cron", it operates only on absolute values

What you could do is create a script that would be triggered by corn every minute and it will remember when it first started, save the last run into a local file, read that file, and only perform the action if that time was 3h before the current time

3
Luuk On

Use:

45 2,5,8,11,14,17,20,23 * * *

see: https://crontab.guru/#45_2,5,8,11,14,17,20,23_*_*_*