To begin with how to setup cron job on your cpanel, you first need to understand what is a cron job, and why is it used. Cron jobs are used to execute a specific function on our application at a specific time or after a specified time interval.
What is Cron Job:
According to Wikipedia Definition:
The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals. The origin of the name cron is from the Greek word for time, χρόνος (chronos).[better source needed] (Ken Thompson, author of cron, has confirmed this in a private communication with Brian Kernighan.) cron is most suitable for scheduling repetitive tasks. Scheduling one-time tasks is often more easily accomplished using the associated at utility.
How to set up cron job on CPANEL or your Web Server?
To setup cron job on your server, please follow these steps:
- To setup cron job, you need to create a separate file specifying each and every action you want to execute at a specific time
- Put this file on the server, in public_html folder
- Open Cpanel and Go to Advanced -> Cron Jobs
- Add an email address, whenever cron executes, it will send an email of the output to this mail id, so you will get notified of the errors in your cron file
- If you don’t want to receive emails every time when cron runs, then you can skip this by appending this to your command: >/dev/null, This will tell the server not to send email when cron executes.
- Add new cron job
- You should see a list of common settings, you have to select a setting you want
- You can skip other fields, as it auto fills the other fields based on the setting you have chosen
- Then write a command like /usr/bin/loj http://www.example.com/cronjob.php, this should be your server exact path
- Then, Click ‘Add New Cron Job’
- You can create as many cron jobs as your requirement.
After you have setup cron job on your server, you should test it if it is working or not. Therefore, you should specify a minimum time when cron runs, while you setup cron job. You should also specify an email address for the email to be received if your cron file has some errors.