skip to Main Content

I have a dedicated server running Cent OS with a Parallel PLESK panel. I need to run a PHP script every second to update my database. These is no alternative way time-wise, it needs to be updated every second.

I can find my script using the URL http://www.somesite.com/phpfile.php?key=123.

Can the file be executed locally every second? Like phpfile.php?

Update:

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

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

My cronjob is set to every minute. I have been running this for some time now in a test environment, and it has worked great. It is really super fast, and I see no increase in CPU nor Memory usage.

6

Answers


  1. 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 script

    #!/user/bin/php
    <?php
        while($i < 60) {
          sleep(1);
          //do stuff
          $i++;
        }
    ?>
    
    Login or Signup to reply.
  2. 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);
    }
    
    Login or Signup to reply.
  3. 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
    <?php
    $start = microtime(true);
    set_time_limit(60);
    for ($i = 0; $i < 59; ++$i) {
        echo $i;
        time_sleep_until($start + $i + 1);
    }
    ?>
    
    Login or Signup to reply.
  4. Have you thought about using “watch”?

    watch -n 1 /path/to/phpfile.php
    

    Just start it once and it will keep going. This way it is immune to PHP crashing (not that it happens, but you never know). You can even add this inittab to make it completely bullet-proof.

    Login or Signup to reply.
  5. This is simple upgraded version of nickf second solution witch allow to specify the desired interval in seconds beetween each executions in execution time.

    $duration = 60; // Duration of the loop in seconds
    $sleep = 5; // Sleep beetween each execution (with stuff execution)
    
    for ($i = 0; $i < floor($duration / $sleep); ++$i) {
        $start = microtime(true);
    
        // Do you stuff here
    
        time_sleep_until($start + $sleep);
    }
    
    Login or Signup to reply.
  6. You can run your infinite loop script with nohup command on your server which can work even you logout from system. Only restart or physically shutdown can destroy this process. Don’t forget to add sleep (1) in your php script.

    nohup php /path/to/you/script.php
    

    Now in case you don’t need to use the console while it’s working, it’ll write its output to nohup.out file in your working directory (use the pwd command to get it).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search