How to write a cron job?
The crontab command, found in Unix and Unix-like operating systems, is used to schedule commands to be executed periodically. It reads a series of commands from standard input and collects them into a file known also known as a “crontab” which is later read and whose instructions are carried out. Generally, crontab uses a daemon, crond, which runs constantly in the background and checks once a minute to see if any of the scheduled jobs need to be executed. If so, it executes them. These jobs are generally referred to as cron jobs.
Each crontab or cron job shares the same format:
minute hour day month weekday command
Whether if you create or change a cron job within cPanel or directly in the file, each crontab will use the above format, and each field can contain:
* A Star, = everything
* A single number
* Two numbers separated by a dash, = a range.
* A comma-separated series of numbers or ranges,= any listed value.
40 10 * * 1-5 means 10:40am, Monday through Friday
32 13 * * 0-4,6 means every day except Fridays at 1:32 p.m
One of the most common uses for cronjobs for a user will be to execute a PHP file on a period schedule. Example:
php -q /home/username/public_html/path/to/your/script > /dev/null
These two commands will execute the php script. Note that > /dev/null is on the end of both of these commands. This will silence any errors or output that might be created by the above commands. The cPanel control panel allows for an email address to be entered. If you would like to receive the output from the commands, you enter your email address. However – this last step is optional.