2

I have data coming from API and I want to call it and dump the data in my db every 4 hours (current day data). I used the following :

0   */4 *   *   *

The data in my report is missing some values, I was thinking that the cronjob is called like this :

today       00:00:00  -> should return nothing for today results
today       04:00:00  -> should return data for today
              ....
today       20:00:00  -> should return data for today
tomorrow    24:00:00  -> should return data for today, but this is tomorrow !

So I am thinking now of calling it like this :

today       00:00:00  -> should return nothing for today results
today       03:59:59  -> should return data for today
              ....
today       19:59:59  -> should return data for today
tomorrow    23:59:59  -> should return data for today, but this is tomorrow !

I am not able to achieve this cronjob. Or there is a better way ?

1
  • Why a second? Why not a few minutes and avoid the issue?
    – terdon
    Feb 5, 2017 at 11:41

2 Answers 2

1

Seconds and negative time are not possible in crontab. Simply wait 59 seconds in the 59th minute of the previous hour. Example crontab:

#
# minute  0-59
# hour    0-23
# dom     1-31
# mon     1-12 (or names)
# dow     0-7  (0 or 7 is Sun, or use names)
#
59 23,3,7,11,15,19 * * * sleep 59 && /bin/datadump
3
  • This is what the asker asked for, but it isn't the right solution, because data that was generated between 23:59:59 and 0:00:00 will be missed. Feb 5, 2017 at 21:13
  • @Gilles, exactly Feb 5, 2017 at 21:38
  • Sure, but is this really a problem? Between 23:59:59 and 0:00:00 is only one second! Previous, the whole day was lost.
    – ingopingo
    Feb 11, 2017 at 16:17
0

Unfortunately cron is designed to be more pattern recognition than arithmetic, so something like -1 0 /4 * * * won't work. You can just specify each hour manually with the comma, so this should hopefully have the desired result:

59 59 3,7,11,15,19,23 * * *

Running a second before the hour is a little too trusting of your database, though. I'd personally give it a few more seconds' grace to wrap up (else it'll still dump tomorrow's data).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.