English 中文(简体)
Run a PHP script every second using CLI
原标题:

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.

最佳回答

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);
}
问题回答

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.

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++;
    }
?>

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);
}

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);
}
?>

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





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签