16

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.

neophyte
  • 6,540
  • 2
  • 28
  • 43
Saif Bechan
  • 16,551
  • 23
  • 83
  • 125
  • I guess I'll be the one to ask - what are you doing that needs done every second? That seems like you'll be putting an incredible strain on the MySQL server. – dragonmantank Nov 13 '09 at 00:35
  • 1
    Hi, i am going to run an auction website. On this website you can place auto biddings, these need to be done before the time ends. With the bids people place the ending time extends for some time, so the auctions don't have to end at a specific time. – Saif Bechan Nov 13 '09 at 17:23
  • 2
    @Saif, editing your question to undo the spelling corrections isn't very helpful... – nickf Nov 16 '09 at 05:47

6 Answers6

32

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);
}
Unpossible
  • 10,607
  • 22
  • 75
  • 113
nickf
  • 537,072
  • 198
  • 649
  • 721
  • 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 Nov 12 '09 at 23:30
  • 1
    `set_time_limit` will change the execution time, and is usually not blocked by hosts in my experience. – nickf Nov 12 '09 at 23:35
  • 1
    Can you help me with setting up this Cronjob every minute. Do i do this in PLESK or some other place. – Saif Bechan Nov 12 '09 at 23:44
  • http://www.webhostingresourcekit.com/flash/plesk-8-linux/plesk8linux_crontab.html – nickf Nov 13 '09 at 05:22
  • how can i make it run every 5 seconds ? ++$i is for +1 pre-incrementing., how can i pre-increment by 5 ?, 5+$i isnt working . –  Jan 01 '14 at 16:05
  • 3
    This answer is very helpful, especially the second part. For those wondering why someone wants to run a cron so often; consider my situation, I need to poll trading market data every 2 seconds via an api. The second half of this answer is especially useful, as sometimes the php script which makes the api request will run over 2 seconds if the remote server is busy. – Tim Hallman Jan 15 '14 at 23:25
  • You should also know that `set_time_limit` is not counting sleeping time with `sleep` with a linux system. So example will not stop with `set_time_limit`. See : http://stackoverflow.com/questions/740954/does-sleep-time-count-for-execution-time-limit – Ifnot Oct 29 '14 at 15:45
25

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.

Milan Babuškov
  • 59,775
  • 49
  • 126
  • 179
4

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++;
    }
?>
aristotll
  • 8,694
  • 6
  • 33
  • 53
jW.
  • 9,280
  • 12
  • 46
  • 50
3

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);
}
Ifnot
  • 4,914
  • 4
  • 33
  • 47
1

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);
}
?>
hakre
  • 193,403
  • 52
  • 435
  • 836
degenerate
  • 1,224
  • 1
  • 14
  • 35
0

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).

Acuna
  • 1,741
  • 17
  • 20