Please Login or Register

Knowledgebase

How to execute a PHP file from CRON

Creating a Cron Job

To add a CRON Job open your cPanel and navigate to the CRON Manager icon.

XDnet: cPanel - Cron Manager - Icon

Within the CRON Jobs Manager you are provided with 2 options, Standard and Advanced (Unix Style) both are shown below.

These are the Possible Command line Arguments you can use. This will effect the output.

CRON Arguments:

-a Run Interactively
-b | Bind Path for external FASTCGI Server mode
-C Do not chdir to the script's directory
-c | Look for php.ini file in this directory
-n No php.ini file will be used
-d foo[=bar]     Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-f        Parse .  Implies `-q'
-h This help
-i PHP information
-l Syntax check only (lint)
-m Show compiled in modules
-q Quiet-mode.  Suppress HTTP Header output.
-s Display colour syntax highlighted source.
-v Version number
-w Display source with stripped comments and whitespace.
-z          Load Zend extension .
These Arguments can be used with both Standard and Advanced paths.

Standard:

Standard Cron Job Manager is for most users who may not be fully familiar with the *nix enviorment.

Now that you have the Standard Cron Manager open, let’s start create a cron job.

You can set a job to run at a specific interval or at a specific times. Creating a cron job requires only three simple steps:

  1. Enter your email address where it says: Please enter an email address where the cron output will be sent. Every time the CRON Job is executed you will be emailed so you can check for errors.
  2. In the Command to run: field, enter the full path to your script. If you want to run a PHP file, the command will begin with PHP. For example, if you want to run a CRON file to optimise your databases, you’d enter PHP and the path to your cron.php file.

    Your file path will look similar to this:

    php /home/[your username]/public_html/admin/cron.php.

    If you want to run the automatic backup script, which backs up your MySQL database and emails a copy to you, you would enter /bin/sh (since this is a shell script), and the path to your script.

    Your path would look something like this:

    /bin/sh /home/[your username]/etc/upstart_cron_backup.sh

  3. Now you need to set the schedule.
    • Set the Minute(s). You can set a short interval here, or minutes of the hour (If you don’t care about minutes, leave this set to 0).

      If you want to test things, set this to run Every Five Minutes. This is a good interval for running the script, checking your email for errors, and changing settings before your inbox gets inundated with cron messages.

    • Set the Hour(s). If you’re creating a cron job to run RSS Importer, you might want to set this to Every Hour. If you’re setting up a backup script, you might choose an hour in the middle of the night for nightly backups.
    • Set the Day(s). You’ll probably want to leave this set to Every Day, but you can also choose specific days of the month.
    • Set the Weekday(s).
    • Set the Month(s).

XDnet: cPanel - Cron Manager - Standard Options

Once you have set times, Click "Save Crontab"

And your done!

Advanced (Unix Style):

The Advanced CRON Job Manager is for more advanced users (Suprised?).

XDnet: cPanel - Cron Manager - Advanced (Unix Style) Options

Adding CRON Jobs using the Advanced Cron Job Manager is the same as using Standard up till Step 3, when it comes to setting the time of execution.

XDnet: cPanel - Cron Manager - Advanced (Unix Style) Options

So the COMPLETE CRON command can be written as:
[ Minute - Hour - Day - Month - Weekday ]  [PATH OF PHP] [ARGUMENTS] [PATH OF PHP SCRIPT]

The timing is spedified using * symbols


* * * * * => Execute every minute
0 * * * * => Execute every Hour
0 0 * * * => Execute every mid-night
0 0 0 * * => Execute every Month
0 0 0 0 * => Execute every Weekday

Since this is a UNIX command, You will have to Mention the PATH of PHP. 

PATH TO PHP  : /usr/local/bin/php

NOTE: CRON Jobs which run more frequently then 10 minutes can hog server resources, and may break the Terms of Service (term 4.1.2) if you need to run a cron job more regularly than 10 minutes apart it is advised you inform support, for any further information please feel free to contact us via ticket or email at sales@xdnet.co.uk






Was this answer helpful?
Add to Favourites Add to Favourites   Print this Article Print this Article
Also Read
cPanel Shortcuts (Views: 991)
Soholaunch Site Builder (Views: 1040)
Accessing cPanel (Views: 1866)
Site Software (Views: 1051)

Powered by WHMCompleteSolution

Language:


Web Hosting
Client Login
Web Hosting

Email

Password

Remember Me


Web Hosting
Search
Web Hosting




Follow Us on Twitter