Question

I currently have a system where on the client side this Update function is called every X amount of seconds:

var Update = function(){
    $.getJSON("PHP/Update.php",{
        userid: userID },
        function(json){
               doSomething();
    });
}

In the PHP file some values are updated and these new values are sent back to the client.

<?php
    session_start();
    include 'dbconnect.php';

    $userid = $_GET['userid'];

    //Change some values in the database
    //Add these new values to $json array

    echo json_encode($json);
    mysqli_close($link);
?>'

This works great when there is exactly one client active at all time. Not so much when there are multiple or no clients active. So what I want is that this PHP files gets called once every X amount of time, regardless of how many clients there are. I realize I probably have to create a loop in PHP but I just can't get it working. Can anyone tell me how I'm supposed to do this? Thanks in advance.

Était-ce utile?

La solution

The tool you use to schedule things in unix-style operating systems is cron. If your server runs MS Windows, you'll need to use some other scheduler.

There are a couple of ways that you can use cron to run your script.

First off, if you have a CLI version of PHP available (as opposed to an Apache module or a fcgi version), you can run it directly. On my system, a cron job to run the script once per minute might look like this:

* * * * * /usr/local/bin/php /path/to/your/script.php

Note that on your system, the php binary might be located in /usr/bin/ or /opt/local/bin/ or somewhere else. You'll need to check with your operating system's package repository to find out where, or type which php from a terminal.

If you use a "shebang" at the beginning of your script, you can get away with a simpler cron job:

* * * * * /path/to/your/script.php

More information about running PHP shell scripts can be found here. A word of warning though... If you launch scripts directly like this, they will run as the user who owns the cron job, which may have security (or at least permission) implications. If you don't understand what this means, please ask more questions before doing it!

If you don't have a command line PHP available, you can still have the PHP script run via cron, by fetching it from a command-line HTTP client like curl or wget or fetch (depending on your operating system). For example:

* * * * * curl -o /dev/null http://example.com/path/to/script.php

or

* * * * * wget -O /dev/null http://example.com/path/to/script.php

On a unix system you can man crontab for instructions on how to install cron jobs, and probably man 5 crontab (depending on your operating system) for instructions on the format for setting specific times. Note that cron uses 1-minute granularity. If you want greater frequency than that, you can fake it. For example, for three times per minute:

* * * * * /path/to/your/script.php
* * * * * sleep 20; /path/to/your/script.php
* * * * * sleep 40; /path/to/your/script.php

And if you want lesser frequency, you use crontab's documented format. For example, every five minutes on weekdays but every 10 minutes on weekends:

*/5  * * * 1-5 /path/to/your/script.php
*/10 * * * 6-7 /path/to/your/script.php
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top