Guide to Cron Jobs
What Is a Cron Job?
cron is a Linux utility which schedules a command or script on your server to run automatically at a specified time and date. A cron job is the scheduled task itself. Cron jobs can be very useful to automate repetitive tasks.
For example, you can set a cron job to delete temporary files every week to conserve your disk space. Some programs, such as Drupal, may even require you to set up a cron job to perform certain functions.
Scripts executed as a cron job are typically used to modify files or databases. However, they can perform other tasks that do not modify data on the server, like sending out email notifications.
If you wish to setup a cron job, remove, or edit, follow the steps provided in this article:
Need more information on cron jobs? Click this link:
Cron Job Elements
Most cron jobs include three components:
- The script that is to be called or executed.
- The command that executes the script on a recurring basis (typically set in the cPanel).
- The action or output of the script (which depends on what the script being called does).
Most scripts that require the use of a cron job will give you specific setup instructions. If you are unsure, check with the author of your script before adding a cron job.
Adding Cron to Under Linux or Unix
Cron Daemon - It is a computer program or a long-running process that runs commands as a background process, instead of being directly controlled by an active user. The daemon regularly checks the file:
and the location of the files or directories:
You need to add Crontab commands for you to make changes to the cron jobs.
The crontab command is used to edit/create a crontab file, list, and remove files, install, and deinstall the cron jobs in Vixie Cron. The files in /var/spool/cron/crontabs are not to be edited directly, even though each user owns their crontab file.
You can use crontab command for editing or setting up your own cron jobs.
Note: This is permanent and there may not be a prompt. Running crontab -r without a user will remove ALL the default users crons.
- To show other user’s cron jobs, combine -l with -u option.
- To edit other user’s cron jobs, combine -e with -u option.
- To remove other user’s cron jobs, combine -r with -u option.
Crontab File (Time Intervals)
Cron jobs can be set to run by the minute, hour, day of the week, day of the month, month or any combination of these.
One of the most common issues with cron jobs in support is that they call for the default versions of PHP on the server, unless otherwise specified. For example, if the client is running a script that requires PHP 7.1, rather than run php, /home/path/to/script.php, the agent could use phpvers command in EIG shell to match the direct path.
To schedule tasks with cron, check out these articles:
- Setting a Cron to Run at a Specific Date and Time
Cron Permissions or Access
Cron allows you to assign who can execute or not execute any file. It can be done by the following files:
Cron allow - /etc/cron.allow
Cron deny - /etc/cron.deny
Things to remember:
- Add the name before the file, whether you want to allow or deny this specific name in using cron.
- If you don’t want anyone to use cron, add the line ALL before the cron.deny file.
- If both files do not exist, only the root user can use cron.
- If both cron.allow and cron.deny have the user’s name, then the user still may use cron.
- If cron.deny is present but is empty, all users can use cron.
- If there’s a user in cron.deny, but there’s no cron.allow file, then ALL users can use cron, but not the ones listed in cron.deny.
Backup installed cron jobs entries
To backup your cronjobs, type the following command to a nas server at /nas01/backup/cron/users.root.bakup directory:
crontab -l > /nas01/backup/cron/users.root.bakup
# crontab -u userName -l > /nas01/backup/cron/users.userName.bakup
Cron Job Limits
Shared and Reseller
- You may NOT run a cron job more often than every 15 minutes.
VPS and Dedicated Server
- You may run a cron job as often as desired.
Magento - To know more about Magento and Magento Cron, check this article: Optimizing Magento
WordPress - This is the recommended article to read for this type of CMS: How To Optimize a WordPress Installation
cURL, GET, and WGET
- cURL, GET and Wget are often used in cron jobs and are enabled on all HostGator servers by default. No further action to activate these functions are necessary. Additional information is available here.
The following articles are for reference only and links will open in a new tab. Please note that HostGator cannot offer direct support for the information provided below.