Multiple Launches With One Cron Schedule
When you have several scripts that need to run at a specific frequency — a daily run of a script to compile stats from a log file is one example — setting up one cron instead of many may save time and frustration.
What you do is install the script below. In a separate file, list the URLs of the scripts that shall be run. Set up cron to run the script every day (or other schedule). When the script below runs, it does an HTTP request to each URL in your list, one URL at a time.
Unlike cron, this system lets you run scripts at any public URL at any domain, whatever PHP or other scripts that a browser or bot can access. Cron requires scripts to be available on the same server where cron is set up.
Most hosting companies provide a dashboard method for you to set up cron for your script. If your hosting company does not, articles Cron and Using Cron have information you may be able to use (warning, highly technical).
Here is the source code. One customization is required, talked about further below.
<?php
/*
Run All URLs
Version 1.0
March 17, 2024
Will Bontrager Software LLC
https://www.willmaster.com/
*/
// Specify the location of the file with the URLs to run.
$FileWithURLs = 'subdirectory/urls2run.txt';
// End of customizations.
$listOfURLs = array();
if( file_exists($FileWithURLs) ) { $listOfURLs = file($FileWithURLs); }
else { echo "File $FileWithURLs not found."; }
foreach( $listOfURLs as $url)
{
$url=trim($url);
if( preg_match('!^https?://!i',$url) )
{ file_get_contents($url); }
}
echo 'OK';
?>
Customization —
The above script will look for a text file (see next paragraph) that contains URLs to run. The text file can be anywhere on the server that could contain web pages.
When you have decided where to put the text file and what to name it, then replace subdirectory/urls2run.txt
with the file's location. The file is not required to have a .txt
file name extension; it can have any extension you wish to give it or even no extension at all. In other words, it needs to be a text file but you can give it any file name you wish to give it.
Upload the above source code as script runURLs.php
or other *.php
file name. Make a note of its URL.
Put the URLs of scripts you want to run into the text file specified in runURLs.php
(see further above). URLs in the text file should be one URL per line. Script runURLs.php
will ignore any lines that do not begin with http:// or https://.
Note: Do not specify the URL of runURLs.php
in the text file containing URLs to run. You would end up with an infinite loop.
Set up a cron schedule for runURLs.php
. Whenever runURLs.php
runs, it will run the URLs in your text file as HTTP requests.
This system can put any URL on the internet on a scheduled run. URLs can be added and removed by updating the text file.
(This content first appeared in Possibilities newsletter.)
Will Bontrager