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