Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWebsite Automation

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 Scheduled Script Launches

Cron is a Unix/Linux utility used to schedule scripts to run periodically at fixed times, dates, or intervals. Scripts to run can be written in PHP, Perl, or other languages.

Most hosting companies have control panels to make setting up a cron job easy (meaning easier than doing it at the raw command line for the server). At the control panel your hosting company provides, look for "Cron" or "Cron Jobs" at their menu icons.

This is not a comprehensive article. Instead, it endeavors to provide the minimum you need to know to set up a cron job yourself.

There are two aspects of a cron job to know about:

  1. How to set up the schedule.

  2. How to launch the script

A cron job is set up by specifying the schedule first and then the script to run.

The control panel your hosting company provides is likely to have both of those together for setting up the cron job.

Specifying the Schedule

Depending on the control panel the hosting company provides, setting up the schedule can be an easy breeze — or it will take some knowledge to set it up.

Generally, the cron job interface that hosting companies provide includes dropdowns to select the day, hour, and minute that your script is to run. If no such control panel is provided, this page at Linuxize.com has a detailed description.

Specifying the Script to Run

The following assumes a PHP script. Setting up Perl and other scripts is similar.

There are two ways to run a PHP script as a cron job: (1) Run it directly from the server. (2) Run it with its URL as if it was called with a browser.

Run directly from the server —

To run the script directly from the server, this is the format for specifying the script.

location_of_PHP location_of_script

The location_of_PHP value is the location where the PHP handling software is installed. It looks something like this:

/usr/bin/php

Your hosting company can tell you the correct location for your hosting account.

Or, if your account is set up to allow the "which" command via a PHP script, you can determine it for yourself. Put this one-line script on your server (named any file name that ends with .php) and then type the script's URL into your browser.

<?php echo `which php` ?>

Note: What appear to be apostrophes or single quotes in the above code are actually backtick characters. On standard English-language keyboards, the character is found at the same key as the tilde character. (A tilde looks like a squiggly hyphen ("~").)

If your hosting company allows the "which" command to run via a PHP script, the script will print the location of PHP on your computer screen when launched with your browser.

After location_of_PHP, there is a space character and then location_of_script. The location of the script is the server location. Generally, that would be the location that your FTP or SFTP software indicates as the location. It might look something like this:

/home/username/public_html/subdirectory/script.php

If the location of the PHP script remains elusive, put a script with the following line of code into the same directory as the script that will be run with cron.

<?php echo __FILE__ ?>

Upload that one-line PHP script and then type the script's URL into your browser. The script will print its server location in your browser window. Update the location printed in your browser window with the name of the script to be run with cron.

You now have both location_of_PHP and location_of_script for your cron command. Example:

/usr/bin/php /home/username/public_html/subdirectory/script.php

Run with its URL as if it was called with a browser —

Some PHP scripts run differently when run from the server in the way presented with the above instructions than they would if run with their URL.

When run from the server, there is no document root location, for example, because there is no public document directory like there would be with a URL. The $_SERVER array is quite different.

When you run into a situation like that, the script needs to run with its URL.

During the couple decades of my experience dealing with servers, I have seen certain methods work at some hosting companies and other methods at other hosting companies. To run a script with a URL as a cron job, try these three methods to find one that works for you at your hosting company:

GET https://example.com/directory/script.php

wget https://example.com/directory/script.php

curl https://example.com/directory/script.php

If none of the above work, a helper PHP script can be made for the cron job. The helper script is run directly from the server.

When the helper script is run by the cron job, the helper script runs the desired script by launching it with its URL.

Here is a helper script that can be run by a cron job. (These instructions assume you name it script.php.)

<?php echo file_get_contents('https://example.com/ScriptToRunWithURL.php'); ?>

In the above code for script.php, replace https://example.com/ScriptToRunWithURL.php with the URL of the script to run.

Then, run script.php with cron using this format:

/usr/bin/php /home/username/public_html/subdirectory/script.php

When script.php is run, it will run ScriptToRunWithURL.php with its URL.

As noted earlier, there are two parts to a cron job, the scheduling and the script location.

The scheduling generally is made user-friendly at the domain control panel provided by the hosting company. But it takes a bit of attention to figure out exactly how to specify the script location.

(This article first appeared with an issue of the Possibilities newsletter.)

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