Guide to Cron Jobs
What is a Cron Job?
A 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 for automatic repetitive tasks. Knowledge of Linux commands is required to use cron jobs effectively.
For example, you can set a cron job to temporarily delete files every week to conserve your disk space. Some programs, such as Drupal, may require you to configure 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 don't modify data on the server, like sending out email notifications.
In this article, we will discuss the following topics about cron jobs.
- Cron job elements ⤵
- How to create Cron on Linux or Unix ⤵
- Cron permissions or access ⤵
- Backup installed Cron job entries ⤵
- 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:
/etc/crontab
and the location of the files or directories:
/etc/cron.*/
/var/spool/cron/
/var/spool/cron/crontabs
You need to add Crontab commands for you to make changes to the cron job. Please visit the following articles for more information about managing cron jobs.
What is a Crontab command?
The crontab command is used to edit or create a crontab file, list and remove files, and 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.
-
crontab -e
-
crontab -l
-
crontab -r
Note: This is permanent, and there may not be a prompt. Running crontab -r without a user will remove ALL the default user's crons.
-
crontab -u
- 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 -v
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.
Field | Value | Description |
---|---|---|
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)
Crontab Operator | Description |
---|---|
* (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 |
Crontab variables
Here are some of the commonly used cron variables.
Cron Variable | Description |
---|---|
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
Cron job limits
Shared and Reseller hosting - You may NOT run a cron job more often than every 15 minutes.
VPS and Dedicated hosting -You may run a cron job as often as desired.
CMS crons
Magento - To know more about Magento and Magento Cron, check the 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.
Additional References
Here are some articles from our HostGator Support page that you may find helpful.
- How do I Create and Delete a Cron Job?
- What do I Put for the Cron Job Command?
- How to Replace WordPress Cron with a Real Cron Job
- How to Set a Cron Job to Use a Specific php.ini File
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.