Search code examples
javascriptphpcron

PHP from Cron job doesn't run a simple Javascript code :(


I'm running an API to retrieve information. I have to call this API via php 5 times, but each time I have to wait 60 seconds. So the PHP File is running for like 6 minutes and gets timed-out. I tried extending the time limit but that doesn't work so I thought of another solution.

Since I have to run this PHP anyway on CRON job, here is the setup:

-- A.php is run every 10 minutes scheduled in Cron manager. This now runs the header("B.PHP?round=1") command and loads B.PHP

----   B.PHP runs, does what it needs to, now uses javascript setInterval waits 60 seconds and loads (window.location.href ="B.PHP?round=2" again with new parameter (to run 2nd or 3rd etc api token).

THE problem is, it never does load the B.PHP again for second round. I tried doing ajax query xmlhttp all type of JS script to load a page.....NOTHING! It seems to either ignore the javascript completely, or just ignores applying the JS code that loads b.php with new parameter


I really don't want to use the sleep(60) method (well it times out anyway). and I have to use Cron job and I know javascript is the only way to make the script just chill during a wait without causing timedout. Any solutions at all? Please guys..be gentle I'm a biiit new at this stuff and know nothing about linux/ubunto :(

ps: The B.php I have the entire URL still doesn't work. I HAVE To call a PHP file from the cron manager.

I KNOW javascript is only on client side, but, the JS code is...loading a file on the server .. ? Ugh...I don't know what to do :/


Solution

  • As you said correctly, JavaScript is only client side.

    Also, cron jobs usually only request a given URL but do not do anything with that result. And they obviously do not execute javascript.

    You need to place the whole logic into your PHP code and use cronjobs to "trigger" your script(s).

    • Cronjob 1: Running every 10 minutes: start.php
    • Cronjob 2: Running every 60 Seconds (maybe make it a little more, if your API has a limit of exactly 60 seconds): process.php

    Since you are using only PHP, you need to store your variable somewhere on your server. This could either be a database or a file on your filesystem. You can find a more detailed explanation on how to persist a variable here: PHP store a single variable on the server?. (In my example I use a file as storage)

    prosess.php:

    // number of times the script should be executed
    $maxRounds = 5;
    
    // load $round from your storage
    $round = file_get_contents('store.txt');
    
    if ($round < $maxRounds) {
        // increase round number for the next call
        // you may want to add some checks to determine if the current round was successful before increasing the value
        // depending on how log your round takes, it might be wise to add another variable (eg "working") to the store, so that multiple calls to the process file do not overlap
        file_put_contents('store.txt', $round + 1);
    
        // execute your code using the $round argument
        doRound($round);
    }
    
    else {
        // already done all rounds
    }
    
    

    start.php

    // reset the $round variable
    file_put_contents('store.txt', 0);
    
    

    Keep in mind that this code is not production ready, but it should point you in the right direction :)