skip to Main Content

This might be a very stupid question so please bear with me.

I have a a php script that makes API calls to Shopify.

The entire point of this php script is to print out statements for each customer.

Now it has to run through about 200 customers.
This entire process takes about 15 minutes.

Ordinarily this runs on a monthly basis with a cron job.

But I need to be able to run it manually as well. I just want the page to execute and do everything in the background with my browser or internet connection playing NO role as to whether the complete execution completes.

The cron job runs header_php.php?run=monthly

Is there anyway I can run it manually, make sure it gets a 200 response from the page, and then close my browser tab and ensure that apache does the rest?

I would be executing it via an AJAX call as well.

Another thing, once each statement is done being processed, the script outputs it to pdf and emails it to the customer. So there’s no feedback required from the page when it runs.

3

Answers


  1. on linux

    <?php 
    exec(''.$command.' > /dev/null 2>&1 &');
    ?>
    

    on windows

    <?php
      $shell = new COM("WScript.Shell");
      $shell->run($command, 0, false); 
    ?>
    

    where command would be something like

    php -f $path/to/filename

    if you put that in a page, you can then call it whenever you want and it will spawn a thread that will call apache, but not require the browser to wait for any response.

    Login or Signup to reply.
  2. You can call your script with other script that runs in background the job.

    shell_exec('nohup /usr/bin/php /dir/to/your/script.php > /dev/null 2>/dev/null &');
    

    Then you don’t need to wait to finish the job

    Login or Signup to reply.
  3. Easily doable with simple HTTP headers.

    1. Start output buffering
    2. Output the response, if any
    3. Send Content-length and Connection: close headers
    4. Flush and end output buffers
    5. The browser receives HTTP response
    6. Continue time consuming processing

    This SO anwer nails it (the comments are helpful as well).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search