0

I'm working on an open-source app that will be distributed to users on different hosts. Some of these hosts will have cron jobs disabled for whatever reason.

Now personally, I think having a script that never ends using usleep(X) in a loop, as an alternative to a cron is a stupid idea, simply because of all the talk about usleep() hogging the CPU. But, this is something I'm not entirely sure about.

Would it be okay to use something like this as an alternative for cron jobs? If not, where else should I look?

  • Haven't checked recently, but I seem to recall PHP leaking memory with long running processes. Probably not the most robust solution. TIAS – Lior Cohen Jun 03 '12 at 23:31
  • have you thought about nodejs, I think it's event model will be great for this sort of thing. – RobertPitt Jun 03 '12 at 23:31
  • 1
    @RobertPitt: the man just said he can't rely on cron being available, not sure if NodeJS is the best suggestion here. This doesn't seem like one of those "Bring your own tools" gig and he obviously has some external limitations. Incidentally, I agree, NodeJS sounds like a good fit here if no such limitations exist. – Lior Cohen Jun 03 '12 at 23:32
  • ahh sorry, did not read the top half of the question, why use cron job, why not just check for updates/pulls every hour or after the hour on a page load. – RobertPitt Jun 03 '12 at 23:38
  • google "web cron" or "pseudo cron", not necessarily as a solution, but for ideas. – goat Jun 03 '12 at 23:48

1 Answers1

1

I am not too sure how good of an idea it is to create a fake cron job using PHP's usleep is (and from reading the usleep's PHP page, the comments do make it sound as though it doesn't hog CPU while it is sleeping). However, there is another way.

How timely does this cron job need to be? What you could do is add the following to a web page's <head>.

<script type="text/javascript" src="cron.js" defer="defer"></script>

With cron.js looking something like:

(new Image()).src = 'cron.php?' + (new Date).getTime();

This would load an image once the page has finished loading (the image being cron.php, I add the getTime to prevent caching). The cron.php file would do whatever task needs to be done, if any at that time. Then it would output an image (an empty, transparent pixel perhaps).

If the website gets enough hits, this may be viable even if the cron needs to be ran on a timely schedule.

Also, if you have a table, or some other way of determining whether a task needs to be ran, you could check to see if a cron needed to be ran before including the cron.js file in the <head> of the document.

ian.aldrighetti
  • 378
  • 3
  • 11
  • Why use script that loads an image, and not just use a non displayed image in the first place? – John V. Jun 03 '12 at 23:46
  • An AJAX request could be made instead. This is just how I have seen it done before in such systems as SMF (Themes/default/scripts/script.js on line 555). They probably use an image because it's less JavaScript. – ian.aldrighetti Jun 03 '12 at 23:52