Guide to Cron Jobs
What Is a Cron Job?
cron is a Linux utility that 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 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 set up a cron job, remove or edit, follow the steps provided in this article:
Need more information on how and where to use cron jobs? Please check this article.
To know more, please click the links below:
- Cron Job Elements
- How to Create Cron on Linux or Unix
- Cron Permissions or Access
- How to Add Cron Job in cPanel
- Cron Job Limits
- CMS Crons
- cURL, GET, and WGET
- Additional References
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 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.
How to Create Cron on Linux or Unix
Cron Daemon 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.
What is a Crontab Command
The crontab command is used to edit or 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 the crontab command for editing or setting up your own cron jobs.
“e” means “edit.” This is for the user to edit the crontab file of the present user. It can also be used to create one if nothing exists.
“l” means “list.” This lets you display the contents of a crontab file of the present user.
“r” means “remove.” This is for you to remove the crontab file or the entries of the present user.
Note: This is permanent, and there may not be a prompt. Running crontab -r without a user will remove ALL the default users crons.
“u” means “user.” If you use this alone, it will not work. A valid username has to be written after the command and should be combined with other crontab commands.
- 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.
This will show the last time you made changes to the crontab file. (This command is only available on a couple of systems)
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.
Each entry in a crontab file consists of six fields following this order:
minute(s) hour(s) day(s) month(s) weekday(s) command(s)
These fields are separated by spaces or tabs, with the first five characters as integers and the sixth as the command to execute.
|minute||0-59||Exact minute that the command sequence executes|
|hour||0-23||Hour of the day that the command sequence executes|
|day||1-31||Day of the month that the command sequence executes|
|month||1-12||Month of the year that the command sequence executes|
|weekday||0-6||Day of the week that the command sequence executes|
|command||special||Complete sequence of commands to execute|
Crontab Syntax and Operators
In your cPanel, you may notice that the existing cron jobs look like this:
* * * * * command(s)
|* (asterisk operator)||This means any value or always. If you selected an asterisk in the Hour field, then it means the task will be performed each hour.|
|, (comma operator)||This allows you to specify a list of values for repetition.|
|- (hyphen operator)||This allows you to specify a range of values.|
|/ (slash operator)||This allows you to specify values that will be repeated over a certain interval between them|
Here are some of the commonly used cron variables
|SHELL||The path of the current user’s shell, such as bash.|
|PATH||The list of directories that will be in the search path for cron.|
|The mail storage location of the current user.|
|MAILTO||It sets who gets emailed with the output of each command.|
|HOME||The home directory of the logged user.|
|EDITOR||The default file editor.|
|LOGNAME||The current user's name.|
|TERM||The current terminal emulation.|
|LANG||The current locale settings|
|USER||The currently logged user.|
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 runs a script that requires PHP 7.1, rather than run php, /home/path/to/script.php, the agent could use phpvers command in the EIG shell to match the direct path.
To schedule tasks with cron, check out this article: 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. The following files can do it:
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 Job Entries
To back up your cron jobs, type the following command to create a backup within the /backup/cron directory:
crontab -l > /backup/cron/root.bakup
If you wish to back up a specific user, use the following replacing userName with the user you want to generate a backup for.
crontab -u userName -l > /backup/cron/userName.bakup
How to Add Cron Job in cPanel
- Log in to cPanel.
- In the Advanced section, click Cron Jobs.<
- Under the Cron Email section, make sure that your current email address is valid and accessible.
- Select your preferred settings from the Common Settings dropdown menus.
- Select the time when your cron will run in the minute, hour, day, month, and weekday dropdown boxes.
- Enter your cron command in the Command box.
- Once done, click the Add New Cron Job button.
The Current Cron Jobs section is also displayed on the same page. The Action column gives you the option to either Edit or Delete each cron job.
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 is 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.
Here are some articles from our HostGator Support page that you may find helpful: