Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWebsite Owner Tools

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!

Cron Command Generator for PHP Scripts

When setting up cron to run a PHP script on a regular schedule, you need to know:

  1. The server location of the PHP processor (generally a file named "php").

  2. The server location of the PHP script that cron is to launch.

If you wish to launch the PHP script with its absolute http://... URL, see the Scheduling PHP URLs With Cron Willmaster Library article. However, to launch your script so it runs as a process on the server, not through HTTP or HTTPS, then you'll need the two items of information listed above.

Hosting companies generally provide cPanel, Plesk, or their own version of domain control panel where cron commands can be specified. If yours doesn't, the articles Scheduling with Cron and Using cron have information you'll find useful.

There's a PHP script further below that you can upload to your server. It determines the cron command for you. All you do is give it the URL to the PHP script you'll be setting up to run with cron.

The script was written from a position of frustration. The online documentation of the hosting company where a client hosts their website has incorrect information about the location of the PHP processor.

I could have guessed until I got it right but it was quicker to write a quick PHP script to determine the location.

It worked a treat. And I thought maybe you would have a use for it, too.

Before presenting it here, the script was cleaned up a bit so it's easier for others to read. It was also modified for user-friendly error messages.

And, here it is!

<?php
/* Cron Command Generator for PHP Scripts
   Version 1.0
   Will Bontrager Software LLC
   https://www.willmaster.com/
/*
$LookingFor = "php";
$URL = empty($_SERVER['QUERY_STRING']) ? '' : urldecode($_SERVER['QUERY_STRING']);
if( ! preg_match('!^https?://!i',$URL) )
{
   $filename = preg_replace('!^.+/!','',$_SERVER['PHP_SELF']);
   ExitWithMessage("<p>Append a '?' and URL of script to the $filename URL. Format:<br><br><span style='font-family:monospace;font-size:larger;'>.../$filename?http://URL-to-your-script</span><p>");
}
$locationOfPHP = `which $LookingFor`;
if( ! strlen($locationOfPHP) ) { ExitWithMessage('<p>The `which php` command isn\'t providing required information. It may be a hosting account restriction.</p>'); }
ExitWithMessage("<p style='font-family:monospace;font-size:larger;'>$locationOfPHP {$_SERVER['DOCUMENT_ROOT']}".(preg_replace('!^https?://[^/]*!i','',$URL)).'</p>');
function ExitWithMessage($s='') { echo $s; exit; }
?>

Name the PHP script GetCronCommand.php or other .php file name you prefer.

Upload GetCronCommand.php to your server and type its URL into your browser.

When you do that, the script asks you to append a "?" character to the URL in the browser's address bar and follow it with the URL to the PHP script you're setting up for cron.

When you do that, the browser will respond with the complete command for use in your cron schedule.

Example of Use

Let's suppose GetCronCommand.php is installed in the document root directory and the domain is example.com. This would be its URL:

http://example.com/GetCronCommand.php

The response is information about what the software needs to generate the cron command — the URL to the PHP script that will be scheduled with cron. Let's suppose that URL is http://example.com/php/script.php

(Once you're familiar with the software and don't need the reminder for how to use it, you can skip the first step and just provide the full request URL right away.)

The full request URL in the browser's address bar becomes:

http://example.com/GetCronCommand.php?http://example.com/php/script.php

The response from GetCronCommand.php will be something like this:

/usr/bin/php /home/username/htdocs/php/hello.php

Copy the response and paste it directly into the command box of your cron scheduler.

Optional-to-Know Technical Points

Determining the location of other server software.

GetCronCommand.php may be modified to determine the location of sendmail, perl, and other software on the server. Simply replace "php" (colored blue) with "perl", "sendmail" or other server software file name.

When you do that, the location of the server software will be the left-most part of the result, not the part that specifies the location of a PHP script.

A caveat of the "which..." command.

The "which..." command is colored red in the source code.

Notice that the quotes are backward-slanting characters, not apostrophes. Don't change them. The backward-slanting quotes are required so PHP knows to run the command directly with the server. (On many desktop computer keyboards, the backward-slanting quote character is found on the same key as the tilde character.)

The Tool

The GetCronCommand.php tool is a wonderful addition to the tool chest of site developers. Site owners who don't do a lot of developing, but do set up cron from time to time, will also find it useful to have on hand.

Run the PHP script and get a copy-and-paste command for cron.

(This article first appeared in Possibilities ezine.)

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