Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top