tags:

views:

955

answers:

3

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.

+2  A: 

why not run a cron to do this and in the php file loop 60 times which a short sleep. That is the way I have overcome this to run a php script 5 times a minute.

To set up your file to be run as a script add the path to the your PHP on the first line such as a perl scrip

#!/user/bin/php
<?php
    while($i < 60) {
      sleep(1);
      //do stuff
      $i++;
    }
?>
jW
Doesn't this method consume more cpu power or memory usage
Saif Bechan
What do you mean with run the file as a script?
Saif Bechan
No, sleeping will almost certainly use less CPU than re-loading the PHP interpreter every second.
JW
+7  A: 

You could actually do it in PHP. Write one program which will run for 59 seconds, doing your checks every second, and then terminates. Combine this with a cron job which runs that process every minute and hey presto.

One approach is this:

set_time_limit(60);
for (i = 0; i < 59; ++$i) {
    doMyThings();
    sleep(1);
}

The only thing you'd probably have to watch out for is the running time of your doMyThings() functions. Even if that's a fraction of a second, then over 60 iterations, that could add up to cause some problems. If you're running PHP >= 5.1 (or >= 5.3 on Windows) then you could use time_sleep_until()

$start = microtime(true);
set_time_limit(60);
for (i = 0; i < 59; ++$i) {
    doMyThings();
    time_sleep_until($start + $i + 1);
}
nickf
HI, thank you for the quick response. I was thinking of the same thing, but doesn't this method use more cpu power and memory usage. And i dont know how to change the default php settings for my domain. I assume it has a limited execusion time.
Saif Bechan
`set_time_limit` will change the execution time, and is usually not blocked by hosts in my experience.
nickf
Can you help me with setting up this Cronjob every minute. Do i do this in PLESK or some other place.
Saif Bechan
http://www.webhostingresourcekit.com/flash/plesk-8-linux/plesk8linux_crontab.html
nickf
A: 

I noticed that the OP edited the answer to give his solution. This solution did not work on my box (the path to PHP is incorrect and the PHP syntax is not correct)

This version worked (save as whatever.sh and chmod +X whatever.sh so it can execute)

#!/usr/bin/php
<?
$start = microtime(true);
set_time_limit(60);
for ($i = 0; $i < 59; ++$i) {
    echo $i;
    time_sleep_until($start + $i + 1);
}
?>
degenerate