node.jsexpressnode-cron

Need to run cron job after every 3 hours


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


Solution

  • 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