Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Development and Maintenance

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Running Many With One cron Command

It is possible to run many scripts — CGI, PHP scripts, anything with an http://... URL that can be loaded into a web browser window — on one or more domains with one cron command.

The cron command runs a CGI script, cronBatch.cgi, that runs the other scripts.

This is handy when you have a certain script on several domains, for backups or sending reminders, as examples. There is now no need to set up a cron command for each script. One cron command can run cronBatch.cgi to take care of all those tasks.

The cronBatch.cgi script is called with a cron command. When launched, cronBatch.cgi looks in a plain text file called "cronBatchURLs.txt" for the list of URLs it should run.

cronBatch.cgi then proceeds to run each URL with method GET just like a browser would if the URL was typed into its address bar.

Because the URLs are in a plain text file, the list is easy to maintain. You can add or remove URLs as appropriate.

Here is the cronBatch.cgi script:

#!/usr/bin/perl
#
# Location of file with list of URLs to run:
my $FileWithURLs = 'cronBatchURLs.txt';
########################
use strict;
use LWP::Simple;
my $log = "Unable to open file of URLs, $FileWithURLs";
goto BOTTOM unless open R,"<$FileWithURLs";
my @URLs = split(/[,;\s]+/,join('',<R>));
close R;
$log = '';
for my $url (@URLs)
{
	next unless $url =~ m!^http://\w!i;
	$log .= "Processing: $url\n";
	get $url;
}
$log .= 'DONE';
BOTTOM:
if($ENV{REQUEST_METHOD} =~ /GET|POST/i)
{
	print "Content-type: text/html\n\n";
	print '<html><body><pre>';
	print "\n$log\n";
	print '</pre></body></html>';
}
# end of script

cronBatch.cgi is designed so it can be run from a browser or with a cron command.

When you first install cronBatch.cgi (you may give it a different file name, if you wish), run it from a browser to verify it was installed correctly. When you get no errors and the word DONE appears on the page, installation was correct.

You can now create a cron command to run the script, if you wish to do so.

In file cronBatchURLs.txt, list the complete http://... URLs of everything that needs to be launched when cronBatch.cgi runs. URLs can be separated with one or more commas, semi-colons, and/or white space.

The script will ignore anything that does not start with "http://". You can write notes to yourself, if you wish, so long as no word begins with "http://"

The name of the cronBatchURLs.txt text file may be changed with a corresponding change on line 4 of the cronBatch.cgi script.

The script will dutifully try to run each URL in the list. However, it will not report error messages; it won't report any resulting message or content.

Therefore, it is wise to run each URL in your browser to verify it runs correctly before adding the URL to the cronBatchURLs.txt file.

Should you need to run a different list of URLs at a different time, or if your list of URLs is so large that your hosting company stops the script before it's done, simply install another script/text file set with its own cron command. The new script/text file set will need to be installed in a different directory or named differently than the first set.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC