Php Cron Jobs With Cpanel

15:27PM 10/11/2010, Lập trình web

A cron job allows you to run a certain command at times set by the job. For example, you could set a cron job to delete temporary files every week so that your disk space is not being used up by those files.

Cron Jobs Detailed Tutorial
To access the Cron Jobs Menu, click on the corresponding icon located on the main screen of your cPanel interface.

The Cron Jobs Menu in the x3 theme appears as follows:

There are two different modes you can use to add a cron job: Standard and Advanced (UNIX Style)

Standard Cron Manager
The Standard mode provides a range of pre-set options that you can choose. This is the simplest method and is recommended.

Adding a cron job in standard mode
Step 1: To access the Cron Jobs Menu, click on the corresponding icon located on the main screen of your cPanel interface.

Step 2: Click on Standard to access the Standard Cron Manager.

Step 3: Enter the email address where the cron output will be sent to in the corresponding field.

Step 4: Enter the command you wish the cron job to run in the blank field next to the Command to run label.

Step 5: Select how often you wish the command to be run by selecting an option in each of the boxes.

Step 6: Click on Save Crontab to save your changes and set up your cron job.

Note: The command run by the cron job must be a valid command.

Deleting a cron job in standard mode
When a cron job is no longer needed, you should delete it so the command will not continue to run.

Step 1: To access the Cron Jobs Menu, click on the corresponding icon located on the main screen of your cPanel interface.

Step 2: Click on Standard to access the Standard Cron Manager.

Step 3: Click on the Delete button next to the cron job you wish to remove.

Advanced (UNIX Style) Cron Jobs Mod
This provides the regular UNIX style cron options. It is recommended only for users, who are used to this method of setting cron entries.

Adding a cron job in advanced mode
Step 1: To access the Cron Jobs Menu, click on the corresponding icon located on the main screen of your cPanel interface.

Step 2: Click on Advanced (Unix Style) to access the Cron Manager.

Step 3: Enter the email address you wish the output of the command run by the cron job to be sent to in the corresponding field.

Step 4: Enter the command you wish the cron job to run in the blank field below the Command label.

Step 5: Select how often you wish the command to be run by placing a number or asterisk (*) in each of the boxes.

Step 6: Click on Save Crontab to save your changes and set up your cron job.

Note: An asterisk (*) denotes to run the command at every possible time for that box. For example, a * in the month box would run the command every month.

More information on cron jobs can be found by searching Google for: crontab.

Deleting a cron job in advanced mode
When a cron job is no longer needed, you should delete it so the command will not continue to run.

Step 1: To access the Cron Jobs Menu, click on the corresponding icon located on the main screen of your cPanel interface.

Step 2: Click on Advanced (Unix Style) to access the Cron Manager.

Step 3: Click on the Remove icon next to the cron job you wish to delete.

How to setup Basic Cron Jobs.

Ok. Many people are yelling that they cannot configure the crons properly.
Here is a short tutorial which will help you set them. I have tried my best to explain the concept but there can be more advanced configurations.

Basically you will want to run a PHP script file in specific intervals. Suppose you want to execute a php file called maintanence.php every one hour. This is what you do :-

[ This tutorial is for noobs, Dont go into detail. Just do as instructed ]

The CRON Command is in the Following Format

[ Minute - Hour - Day - Month - Weekday ] - Command

The COMMAND, can be broken down in

[PATH OF PHP] [ARGUMENTS] [PATH OF PHP SCRIPT]

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

If you did not understand anything till now.. Good, this means you are a noob.. Read ahead

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

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

( it also same at trap17, if users over there want to use it )

These are the Possible Command line Arguments you can use. This will effect the output. In our case, we will use the -q ( Quiet mode ) argument.

-a Run interactively
-b <address:port>| <port></port> </address:port> Bind Path for external FASTCGI Server mode
-C Do not chdir to the script's directory
-c <path>| <file></file> </path> 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 <file></file> Parse <file>. Implies '-q'</file>
-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 <file></file> Load Zend extension <file>.</file>

The Path of the PHP file Must be Complete absolute path.
If you have an Account at TRAP, And your USERNAME is “tom”, Your path will be
“/home/tom/public_html/”

I assume you are familier with the PUBLIC_HTML directory. That is the Root folder where you store your html files.

So lets say that Tom wants to execute my script “maintenance.php” every hour. And it is stored in “public_html/cron/maintenance.php”;

So the Complete CRON command would be,

0 * * * * /usr/local/bin/php -q /home/tom/public_html/cron/maintenance.php

If tom wants to execute it every minute, he would use.

* * * * * /usr/local/bin/php -q /home/tom/public_html/cron/maintenance.php

If tom wants to execute it every Month, he would use.

0 0 0 * * /usr/local/bin/php -q /home/tom/public_html/cron/maintenance.php

There are more Complex forms of Assigning the TIMINGS for these scripts. You can go to CPANEL => Cron Jobs => Standard and set exact time when the script will be executed.

Also, this method used php, you can also use Curl.


Theo thuthuat.vn