Create a Cron Job in cPanel – Cron Job Tutorial for Dummies

Cron Job in cPanel

This guide is meant for anyone who wants to create a cron job and for all those who has never ever created a cron job in their life through cPanel.

First of all, what is a cron job ? This is Wikipedia‘s explanation:

Cron is the time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.

Cron is available on Unix, Linux and Mac servers. Windows servers use a Scheduled Task to execute commands.

All the cron jobs tutorials just concentrates on the steps on scheduling a cron job, but most of the the users don’t know anything about Unix/Linux commands or even aabout command-line. So they make mistakes in adding the command to the Command textbox of the cPanel.

Cron Command Text Box of cPanel

PHP is one of the most popular server-side programming language, so I will concentrate on adding a PHP script as a cron job. In addition to that you will be provided with a working(and tested) PHP code to find the path to be mentioned in the cron command textbox.

Open your text editor and paste the following:

Now follow the steps mentioned below to get running…

  1. Save it as path.php
  2. Upload it to the directory where your php file meant for the cron job is located
  3. Now run the file in browser
  4. Copy the output to your text editor

The output will be like

/home/USERNAME/public_html/DIRNAME
or
/home2/USERNAME/public_html/DIRNAME

Now append the filename of the script that you want to run to the output. The final line will now look like /home/USERNAME/public_html/DIRNAME/FILENAME

where

USERNAME - Your cPanel Username

DIRNAME - the directory name where the file resides

FILENAME - the file name of the file that you want setup as a cron job

Now launch Cron Jobs section of your cPanel. It will look like this:

Add a new Cron Job

In the screenshot above I’ve selected daily as my interval and entered the following line of text into the Command textbox.

php /home/USERNAME/public_html/crons/my-cron.php

where

crons - the directory where my php file resides

my-cron.php - the php file to be setup as a cron job

Now click the Add New Cron Job and your are done !

Voila! You’ve scheduled a cron job !

2 Responses

  1. new websites May 9, 2013 / 1:00 AM

    finally i found someone who knows how to provide relevant information on the topic i have been searching for? thanks, at last i can study with pleasure..

  2. Natalie September 3, 2014 / 1:20 PM

    Great post.
    Sometimes external cron job service like https://www.easycron.com built with many enhanced features is handy for use and powerful too.

Leave a Reply

Your email address will not be published. Required fields are marked *