1. Knowledge Base
  2. >
  3. Results
  4. >
  5. Guide to Cron Jobs

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.

A working knowledge of Linux commands is required to use cron jobs effectively.

If you wish to setup a cron job, remove, or edit, follow the steps provided in this article:

How Do I Create and Delete a Cron Job?

Need more information on cron jobs? Click this link: 

How to Replace WordPress Cron with a Real Cron Job


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: 

/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 jobs. 

Crontab Commands

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.

  • crontab -e 
    “e” means “edit.” This is for the user to edit the crontab file of the present user. It can be also used to create one if nothing exists.
  • crontab -l 
    “l” means “list.” This lets you display the contents of a crontab file of the present user. 
  • crontab -r
    “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.

  • crontab -u
    “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.
  • crontab -v
    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.

Note: HostGator limits cron jobs on Shared servers to run in 15 minute intervals, since excessive cron jobs can cause high loads on the server.

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:


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.

CMS Crons

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.

Additional Resources

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.