Run a PHP script every second using CLI

Posted by Saif Bechan on Stack Overflow See other posts from Stack Overflow or by Saif Bechan
Published on 2009-11-12T23:23:18Z Indexed on 2010/03/23 5:33 UTC
Read the original article Hit count: 322

Filed under:
|
|
|

Hello,

I have a dedicated server running Cent OS with a Parallel PLESK panel. I need to run a php script every second, that updates my database. These is no alternative way timewise, i have checked every method, it needs to be updated every second.

I can find my script using the url: http://www.mysite.com/phpfile.php?key=123, and this has to be executed every second.

Does anyone have any knowledge at all on doing this, i can not seem to find the answer.

I heard about doing it with CLI and putty, but i have no knowledge of this at all. Or can this be done using the PLESK Panel?

And can the file be executed locally every second. Like \phpfile.php

If someone helps me on answering these question i would really appreciate it.

Regards

EDIT

It has been a few months since i added this question. I ended up using the following code:

#!/user/bin/php
$start = microtime(true);
set_time_limit(60);
for (i = 0; i < 59; ++$i) {
    doMyThings();
    time_sleep_until($start + $i + 1);
}

Thank you for this code guys!

My cronjob is set to every minute. I have been running this for some time now in a test environment, and this works out great. It works really supperfast, and i see no increase in CPU nor Memory usage.

© Stack Overflow or respective owner

Related posts about cli

Related posts about php