September 27, 2019
5 min Read
September 27, 2019
5 min Read
There are always ways to do things more practically. Handling repetitive tasks using an automated process is much more preferable by busy webmasters. If you use a Unix-like operating system, a cron job could save your time by performing a task automatically.
In this article, we would like to explain the basics of a cron job, and how to use it for scheduling tasks.
Cron is a utility program for repeatedly performing a task at a later time. The act of giving a command to schedule a task at a specific time repeatedly is a cron job.
Here’s how it works:
If you want to schedule a task at a later time for once, you might use another command like it. But, for recurring tasks, cron is the perfect solution.
Cron is a daemon, meaning that it works in the background to execute non-interactive tasks. In Windows, you might be familiar with the background processes such as Services.
A daemon is always in the idle state and waits if a command request to perform a certain task — either within the computer or from any other computers on the network.
A cron file is a simple text file that contains commands to run at a specific time. The default system crontab file is /etc/crontab and is located within a crontab directory, /etc/cron.*/. Only system administrators can edit the system crontab file.
However, as Unix-like operating systems support multiple users, each one can also create their own crontab file and launch commands to perform tasks anytime they want. A cron daemon will check the file and run the command on the system background.
With cron jobs, you can automate system maintenance, disk space monitoring, and schedule backups. Because of its nature, cron jobs are great for a computer that works 24/7 — a server.
Please note that while cron jobs are mostly used by system administrators, it can be incredibly useful for web developers too. For example, to deactivate an expired account, check broken links, or even send newsletters for targeted users.
Basically, you can create and edit cron jobs using a few different methods. In this tutorial, we would like to show you how to do it using the Linux Shell Prompt (Terminal).
If you have a VPS at Hostinger, you can find the login credentials in your VPS management tab, and access your server through SSH. If you’re having trouble, check out our PuTTY SSH tutorial.
Here are some basic operations that cron jobs can perform:
If you want to edit a crontab file of the current user, type crontab -e in your terminal:It would give you a result like this.As it uses the vi editor, you can learn the basic vi commands to make sure you take the right action.
If you want to edit another user’s crontab, you can type crontab -u username -e. Keep in mind that you can only do this as a superuser, meaning that you need to type: sudo su before the command.Another operation allows you to see if there is any crontab file created. You can simply type crontab -l. If you don’t have any, you will see this result.In addition, if you want to see the crontab file lists of other users, you can type crontab -u username -l as a superuser.Apart from knowing the basic operations, it is important to learn the basic syntax too.
Basically, a crontab file consists of two parts: the schedule timer and the command. This is how the command is written:
* * * * * /bin/sh backup.sh
Let’s get into some extra detail.
As previously mentioned, a crontab file has five fields – each field is represented by an asterisk – to determine the date and time of a certain task set to perform repeatedly.
Adding to that, you need to use the proper characters in each crontab file.
Now that you have learned how to write cron syntax properly, we would like to give more examples to help you understand the rules stated above better.
Before moving on, keep in mind that the output of the command will be automatically sent to your local email account. So, if you want to stop receiving these emails, you can add >/dev/null 2>&1 to the syntax as in the following example:
0 5 * * * /root/backup.sh >/dev/null 2>&1
Besides, if you want to send the email output to a specific account, then you can add MAILTO followed by the email address. Here is an example:
MAILTO="firstname.lastname@example.org" 0 3 * * * /root/backup.sh >/dev/null 2>&1
Here are more syntax examples:
|0 0 * * * /bin/sh backup.sh||To perform a database backup at midnight every day.|
|0 6,18 * * * /bin/sh backup.sh||To perform a database backup twice a day at 6 AM and 6 PM|
|0 */6 * * * /scripts/monitor.sh||To perform monitoring every six hours|
|*/10 * * * * /home/user/script.sh||To perform a cron job for the script file located in the home directory every 10 minutes|
|0 * 20 7 * /bin/sh backup.sh||To run a database backup hourly every 20th of July.|
|0 0 * * 2 * /bin/sh||To run a database backup at midnight every Tuesday|
|* * * 1,2,5 * /script/script.sh||To run a command on January, February, and May|
|10-59/5 5 * * * /home/user/script.sh||To run a command every 5 minutes at 5 AM, starting at 5:10 AM|
|0 8 1 */3 * /home/user/script.sh||To run a command quarterly on the 1st day at 8 AM|
|* * * * * /scripts/script.sh; /scripts/scrit2.sh||To set a schedule for multiple tasks on a single cron job|
|@reboot /scripts/script.sh||To perform a certain task every time you start the system|
Setting an automatically scheduled task will not only be practical but will also prevent missing the supposed action to take on time.
Cron Job is a great way to manage such tasks either as a system administrator or any professions like a web developer. All you need to do is to use the right command and choose the right time.
Here are some of the basic commands:
Not only that, understanding the right characters would help set the specific time of the intended schedule:
So now, let the automation work for you at any time you prefer!