Cron is a Linux command for scheduling a command or script on your server to complete repetitive tasks automatically. 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.
For example, you can set a cron job to delete temporary files every week so that your disk space is not being used up by unnecessary files. Some scripts, such as Drupal, may even require you to set up a cron job to perform certain functions.
Cron jobs can be a very resourceful tool as they can be set to run by the minute, hour, day of the week, day of the month, month or any combination of these. Excessive cron jobs can cause unnecessary loads on the server.
Warning: It is important that you familiarize yourself with how cron jobs work. You need to have a working knowledge of Linux commands before you can use cron jobs effectively.
For most cron jobs, there are three components present:
- The script that is to be called or executed
- The command that executes the script on a reccurring basis (typically set in DirectAdmin)
- 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 instructions on what needs to be set up. If you are unsure, check your script with your hosting administrator before adding a cron job.
Setup a Cronjob
To access cron jobs, click on the Advanced Tools icon from the main control panel menu, then click the Cron Jobs icon.
Cron jobs are system commands that can be scheduled. For example, if you wanted a CGI script to run every Monday morning, you could do this through the cron jobs menu.Values that can be set are minute, hour, day, month, and day of week (Monday-Sunday; the numbers 0 or 7 represent Sunday). Any value can be ignored by placing an asterisk (*) in the text field.
In the above example, we are setting updater.cgi to run every day at midnight.
- You can also specify exact times using commas to separate them (e.g. 1,2,3 is minutes 1, 2 and 3).
- You can specify spans using a dash (e.g. 5-7 is minutes 5 to 7).
- You can specify intervals using a star and a forward slash. (e.g.: */2 is every 2nd minute.)
- You can combine them to create a more precise schedule. (e.g.: 1,5,11-15,30-59/2 (minutes 1, 5, 11 to 15 and every 2nd minute between 30 and 59).
Remember to click Add when you are finished entering your data.
If you're trying to run a php script, remember to run the php binary, and pass it to the php script. For example:
When complete, a basic cronjob will look as follows:
15,45 * * * * /home/domain.com/script/myscript > /dev/null 2>&1
The scheduling was defined as follows:
* * * * * command to be executed - - - - - | | | | | | | | | ----- Day of week (0 - 7) (Sunday=0 or 7) | | | ------- Month (1 - 12) | | --------- Day of month (1 - 31) | ----------- Hour (0 - 23) ------------- Minute (0 - 59)