Cron jobs: Creating and using timed commands
Cron jobs are a software utility and time-based scheduler in Unix-like environments. Though they are found in a range of software, for this article, we will look at cron jobs in cPanel for your website.
They are used in a variety of tasks, such as running scripts or downloading items from the internet. If you have a website and are managing it yourself, you may want to know more about cron jobs. Most pre-built software, such as WordPress, includes its own scheduling features, which means you won’t need to create a cron job to schedule a post.
A breakdown of cron jobs
If you are using cPanel, log into your account, navigate to ‘Advanced‘ and then ‘Cron Jobs‘. Here, the system will give you a brief breakdown of what a cron job is and the ability to create one.
Cron Jobs is a simple system that is broken down into six different input fields. The first five represent time, while the sixth is dedicated to the command that needs to be run when the time stipulated is met. In order from left to right, time in cron jobs is designated to minute, hour, day of the month, month, and day of the week.
Each of these also has a set number range that can be used, which are:
- Minute: 0 – 59
- Hour: 0 – 23
- Day of month: 1 – 31
- Month: 1 – 12
- Day of week: 0 – 6 (Sunday to Saturday) [7 may be used for Sunday as well]
For example, running a cron job at different times will look like this:
Once per minute: (* * * * *) Once per day: (0 0 * * *) On the 1st and 15th of the month: (0 0 1,15 * *)
Cron jobs can also be further separated into seconds, such as the example:
* * * * * /scripts/script.sh * * * * * sleep 30; /scripts/script.sh
This looks different from previous cron job examples as the system isn’t built to run in increments of seconds. Because of this, the system needs to be fooled into running the /scripts/script.sh command every 30 seconds.
Creating cron jobs
In order to create a cron job, you will need to know when the script should run, as well as where the file for it is located. With regards to the file’s location, the cron job/CPanel system understands the root, or home folder of your web server. Because of this, you will not need to write out the entire website name. For example:
* * * * * /scripts/file.php (correct) * * * * * http://www.yourwebsite.com/scripts/file.php (incorrect)
Navigate to ‘Advanced‘ and then ‘Cron Jobs‘ in your cPanel. You will be presented with the option to have the system email you each time a cron job is executed. This is recommended for important tasks you need to keep track of.
Below that is a number of drop boxes for creating a cron job. These contain settings for the most common times to execute a job, such as once per hour, or per day, as well as the option to further customise the code. Due to the ease of use in cPanel, you won’t need to know all of the timing codes from scratch.
The final box is for the command, or the task that needs to be executed. As previously discussed, this can run a range of tasks, such as executing a script or dumping the contents of a system.
Once all of the fields have been filled in, click on the ‘Add New Cron Job‘ button. The job will now be stored in the system, along with any other you create in the future.