Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 510
Saif Bechan Avatar asked Nov 12 '09 23:11

Saif Bechan


People also ask

How do I run a PHP script from the command line?

To run the PHP script from the command line you should just have a PHP CLI (PHP Command Line Interface) installed on your system. This note shows how to run the PHP script from the command line and how to get the PHP CLI installed on Windows, Linux and MacOS.

How to call a PHP file every 10 seconds?

Whilst using cron to call PHP scripts allows for intervals as low as 1 minute sometimes you may need a lower rate, here is how to call a PHP file every 10 seconds. Using Jquery and ajax allows for calling an XHR request every set amount of milliseconds (setInterval).

Why do PHP scripts have a special first line?

On Unix systems, the special #! (or "shebang") first line should be added to PHP scripts so that the system can automatically tell which program should run the script. On Windows platforms, it's possible to associate php.exe with the double click option of the .php extension, or a batch file can be created to run scripts through PHP.

How often does this PHP script write success in console?

This calls call.php every 10 seconds and will write Success in the console each time it runs. To modify this script for some GET request parameters it looks like this: This will call call.php?id=44&key=XJF4YS1JF6H every 10 seconds.


1 Answers

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);
}
like image 65
nickf Avatar answered Oct 22 '22 15:10

nickf