Launch PHP Script in Background
If you have PHP scripts that
- take a long time to run and
- you want to launch automatically from a web page,
consider launching them so they run in the background.
They might be scripts to make a synopsis of the previous day's access logs and send it to you in an email, create or download backups, test forms, or other jobs that can take a long time.
Here is how I implemented the functionality.
My personal portal page is a PHP web page with links and lists and automations.
When I load the page for the first time every day, it launches an external PHP script with a list of jobs to do for me in the background.
It wasn't always that way.
Before I figured out how to launch an external PHP script from a web page and run the script in the background, the personal portal page would never finish loading until the last of the automations were done.
And that would take a long time.
You see, one of the jobs is to download the MySQL backups made during the night.
There are hundreds of backup files to download every morning, total size over a Gig. To prevent server resources from getting tied up for minutes, there is a pause between each file download. With the generous pauses, the total download time is about 15 minutes.
I was unwilling to sit at my desk twiddling my fingers for that length of time.
Something had to be done about that external PHP script.
Short Development Notes
During development, my first try was to launch the external PHP script and have the script reply with a HTTP/1.0 204 No Content
header. My thought was that the page load would then not wait for the script. But different browsers treated the header differently.
In the end, cURL was used, a PHP library generally available wherever PHP is installed.
How It Works
-
The web page being loaded into the browser uses PHP to request a page.
The requested page is the external PHP script. The request is what launches the external script.
-
The web page artificially times out the request to the external PHP page.
The time-out event releases the request connection between the web page and the external PHP page. Each is now independent.
The web page can finish loading. The external PHP script continues to run independently in the background.
In fact, the browser tab with the web page can be closed. Or the tab can be loaded with a different URL. The browser can even be exited.
Regardless what is done with the browser, the external PHP script keeps on running until it's work is done.
The Code to Make It Happen
The web page being loaded uses a PHP function to launch the external PHP script and then disconnect from the script.
Implementation is two parts, the function and the line of code that uses the function.
The PHP Function
The PHP function can be included from an external file or it can be pasted in the web page itself. Either way, this is the code. (Notes follow.)
<?php function LaunchExternalScript($url) { $options = array( CURLOPT_TIMEOUT => 3, CURLOPT_NOSIGNAL => true, CURLOPT_USERAGENT => "Launcher" ); $ch = curl_init($url); curl_setopt_array($ch,$options); curl_exec($ch); } ?>
Notes:
When the LaunchExternalScript()
function is called, it's provided with the URL of the external PHP script to launch.
Optionally, two places in the function code may be customized.
-
The blue 3 at line 5 is the number of seconds to elapse before the launch is timed out. That number may be changed. Make the number of seconds sufficient to ensure the launch is initiated but not so many that it slows down the web page load more than necessary.
-
The
CURLOPT_USERAGENT => "Launcher"
line allows you to specify a custom user agent string.If you wish, that entire line may be removed from the function. But if you do want to specify a user agent string, replace
Launcher
with your own user agent identification.
As noted earlier, the above code can either (a) be the content of a file to be included in the web page or (b) exist in the source code of the web page itself.
Using the PHP Function
The PHP function is used with this line:
<?php LaunchExternalScript("http://example.com/externalPHPscript.php"); ?>
Replace http://example.com/externalPHPscript.php
with the URL of the external PHP script to launch.
The PHP LaunchExternalScript()
function code (the source code further above) needs to be above the point where you use the PHP function.
If the PHP function is a separate file to be included, this is how to include it:
<php include("{$_SERVER['DOCUMENT_ROOT']}/location/thePHPfunction.php"); ?>
Replace /location/thePHPfunction.php
with the location of the file to include.
You now know how to launch an external PHP script to run in the background without inconveniencing your normal web page viewing.
(This article first appeared in Possibilities newsletter.)
Will Bontrager