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.
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…
- Save it as path.php
- Upload it to the directory where your php file meant for the cron job is located
- Now run the file in browser
- Copy the output to your text editor
The output will be like
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
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:
In the screenshot above I’ve selected daily as my interval and entered the following line of text into the Command textbox.
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 !