4

How can i find out programmatically when the next cron will run in order to present it to the author scheduling stuff?

i am using ultimate cron and an external cron process.

  • What software are you using to make cron run happen? Built in mechanism once called "poor man's cron"? External cron? – Mołot Jun 27 '13 at 8:00
  • I am agree with @Mołot, though you can get last cron run time. – RajeevK Jun 27 '13 at 8:15
  • @RajeevK - it's nice to know, sometimes, but it's useless for content authors. Like "We don't know when what you added will be visible, but last time we synced was an hour ago" - frustrating. – Mołot Jun 27 '13 at 8:17
  • Ultimate Cron handles cron runs, it does not trigger them. If you are not using any other tools, see first case of my answer. – Mołot Jun 27 '13 at 8:19
  • @Mołot Though I have never worked with cron much except declaring my own work for cron in module, but I knew we can get the last time when it ran. Somethings are really frustrating, should look simple but take days to make it work according to us :) – RajeevK Jun 27 '13 at 8:22
3

Basically, you cannot.

If you are using Drupal 7 built-in CRON triggering method, in Drupal 6 Poormanscron module, CRON run depends on the availability of visitor. If no one visits your page, CRON will not run. Drupal can't predict if or when visitor will appear, so the best you can know is when CRON will not yet run anyway, even if visitor is available.

If you are using external tools, Drupal has no way to predict if and how they will trigger it. It will accept all proper request, so you know even less than in the first case. Even if you know when it is supposed to run, you don't know time difference between servers, as it is subject to change slightly. You don't know the lag between a moment when CRON should happen and when it will, as practically no web servers are hosted on real time operating systems, so there are no guarantees about maximum time between action request and it's execution. In simpler words, server can postpone CRON task if it's to loaded, sometimes even skip it.

If you need predictable and reliable cron, go to http://example.com/admin/config/system/cron, find URL described as "To run cron from outside the site, go to". Then, mail your server administrator and ask him to add this to server's crontab:

*/5 * * * * wget -O - -q -t 1 http://www.example.com/cron.php?cron_key=yourcronkeyrandomstring

This will make your site's CRON run every 5 minutes, and you shouldn't need to know when exactly - it's ofthen enough for most tasks. If it's not, make it */2 to run it every 2 minutes, or just leave * at every position to get it triggered each and every minute. You can read more about system's CRON configuration on Wikipedia. Of course it will not allow Drupal to know, it will just free you from the need to know.

  • 2
    Good answer. I'd go so far as to say that if you're trying to predict when a cron run is going to happen, you've either got something wrong in your application design, or you're not running cron often enough – Clive Jun 27 '13 at 8:06
  • I agree. On largest projects we were using */5 minutes external cron and Elysia Cron to limit tasks we didn't need quite that often. Worked like a charm. – Mołot Jun 27 '13 at 8:08
  • In OP I'm assuming "external cron process" means cron is being kicked off at regular intervals by crontab or something. – Johnathan Elmore Jul 2 '13 at 20:30
  • @JohnathanElmore First, it was edited only after my answer. Second, it means it is supposed to be kicked at regular intervals, not that it actually is. – Mołot Jul 2 '13 at 21:29
3

The easiest method here (though by no means a good, reliable one) is to set up the cron job to run at a known time (let's say hourly, on the hour) and then take that knowledge back to Drupal where you can do something low-tech like

$minutes_left_until_next_cron = 60 - date('i', REQUEST_TIME);

Of course this relies on things like the web server and external cron servers' system times being synced correctly, and isn't really an elegant solution. But, it'll do in a pinch.

  • 1
    Yes, in an ideal world, where the interval is a standard value... thanks! – Pan Chrono Jun 27 '13 at 8:40
  • It'll give good guess, not knowledge. I guess in my answer I should have accepted that for most people good guess is acceptable. – Mołot Jun 27 '13 at 8:43
  • @Clive - to avoid sync problem, use variable_get('cron_last'); If it;s supposed to happen every hour, make it (60 + $cron_last - $minute_now)%60. That way, if ΔT is constant, prediction will be a bit better, and only assumption needed is how often cron runs, not at what minute in an hour. – Mołot Jun 27 '13 at 8:53
  • 1
    Only problem with that is cron_last gets set after a potential 3 minute long cron run. When you invoke a cron at 10:59, and it records cron_last as 11:02, that method would be an hour out-of-whack. But I'm just being petty (as we programmers oh so love to be ;) – Clive Jun 27 '13 at 8:58
  • 1
    @Clive - Ultimate and Elysia logs start time too. Both ways of guessing have their ups and downs, constant error equal to servers ΔT, theoretically up to an hour, or small error occasionally going huge, to an hour. Readers will need to choose problems they prefer from these methods ;) – Mołot Jun 27 '13 at 9:08

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.