How to Set Up cron Jobs in cPanel

Feb 11, 2023 | News

Cron jobs in cPanel

How to Set Up cron Jobs in cPanel – Syntax, Commands, Scheduling Options Tips Explained

Cron jobs in cPanel can be an incredibly useful tool for automating repetitive tasks and streamlining processes. Cron is a time-based job scheduler that enables users to schedule commands or scripts to run at specified intervals, such as every hour or day of the week. This allows cron jobs to perform operations on a regular basis without having to manually initiate them each time they are needed. In this article we will provide you with examples and how to set up cron Jobs in cPanel

A few examples of cron job usage

Backing up a database:

0 0 * * * /path/to/script/database_backup.sh 

This cron job will run the script /path/to/script/database_backup.sh at midnight (0 0) every day (* * *).

Sending email reports

0 9 * * 1 /path/to/script/generate_and_send_report.sh 

This cron job will run the script /path/to/script/generate_and_send_report.sh at 9:00 AM (0 9) every Monday (* * 1).

Updating a website

0 3 * * 7 /path/to/script/update_website.sh 

This cron job will run the script /path/to/script/update_website.sh at 3:00 AM (0 3) every Sunday (* * 7).

Cleaning up logs

0 0 * * * /path/to/script/clean_up_logs.sh 

This cron job will run the script /path/to/script/clean_up_logs.sh at midnight (0 0) every day (* * *).

Refreshing a cache

*/15 * * * * /path/to/script/refresh_cache.sh 

This cron job will run the script /path/to/script/refresh_cache.sh every 15 minutes (*/15).

Note: The crontab syntax consists of six fields, separated by spaces, that specify the minute (0-59), hour (0-23), day of the month (1-31), month (1-12), day of the week (0-7, where both 0 and 7 represent Sunday), and the command to run.

To configure cron jobs in cPanel, it is important to understand cron syntax and the crontab command. The crontab command is used to view, edit and create cron jobs; it consists of five fields which represent minutes, hours, days, months and day of the week respectively.

Environment variables can also be used to extend cron scheduling options, allowing users to specify additional parameters such as the location of the script being executed or when cron should run (for example, in a specific timezone).

Warning:ย You need to have a good knowledge of Linux commands before you can use cron jobs effectively. Check your script with your hosting administrator before adding a cron job.

How to setup a cron jobs in cPanel

To setup, a cron job in cPanel you go to cPanel > Advanced – Cron Job

The first thing you will see is a field to set up the Cron Email, when your cron job is providing output it will send this to your email address.

set up cron job in cpanel

If you do not want an email to be sent for an individual cron job, you can redirect the commandโ€™s output to /dev/null. For example: my command >/dev/null 2>&1

Then you provide how often or when the cron job needs to be scheduled. For example once per hour, every 15 minutes, or every 2nd day of the week.

cpanel cron job scheduling

At 040Hosting we do not allow cronjobs that run every minute on our shared hosting environment, as each cron job is using resources and to avoid cron jobs causing massive issues on the server we prefer you only create cronjobs for every 10 or 15 minutes at the maximum. Do you need faster scheduling? i.e. every 5 or 1 minute please contact support and we can work together to find a solution. Cron jobs set to every 5 or every minute without permission will be removed.

The cPanel cron job page will guide you through the different options.

At the command field of the form you can fill in the command which you want to start at the scheduled time(s).

And that is it, you have set up your cronjob and it will start running at your specified interval.

Nee further assistance with setting up a cronjob or looking for specific examples, let us know, we are glad to assist.

Questions? We can help.

Translate ยป