🇺🇸 What are cron jobs? Learn in a simple way
Portuguese version: 🇧🇷 O que são cron jobs? Aprenda de forma fácil
Youtube video: https://youtu.be/g69c8iUHhXk
Hello everyone, today I bring to you a very interesting and useful tool in the development world: the cron jobs. So, let’s start!
What is a cron job?
First let’s separate and understand the terms: cron and job. In a coding context, a job can be interpreted as a simple task, a piece of work required to be done. And cron is a scheduler tool present in Unix systems. So, the cron job can be understood as an automatically scheduled task. You can use cron jobs to automate repetitive tasks, like updating software, clearing cache, creating backups, etc.
Creating a cron job using the terminal
On Unix systems, cron is a daemon, a program that runs in the background. You can manage your cron jobs by editing the cron file in your SO. On my SO (Linux Mint), the file is located on /var/spool/cron/crontabs/<your-user>
. But there is a command to facilitate editing this file, it is crontab -e
(crontab
is a short version of cron table). Running this command, the file content will be displayed using your default editor, in my case is Vim. Now, you can add your cron jobs definitions at the end of the file. The format of the definition looks like this:
* * * * * <command>
The specified command will be executed in the interval defined by the cron syntax * * * * *
, in this case will run every minute. Ok, let’s understand what this syntax means.
Each asterisk represents a time value, in this sequence:
(minute) (hour) (day of month) (month) (day of week) <command>
Bellow there are some examples to facilitate the comprehension:
0 0 * * 0 /root/backup.sh | Perform a backup every Sunday at midnight. |
---|---|
0 * * * 1 /root/clearcache.sh | Clear the cache every hour on Mondays. |
0 6,18 * * * /root/backup.sh | Backup data twice a day at 6 am and 6 pm. |
*/10 * * * * /scripts/monitor.sh | Perform monitoring every 10 minutes |
These examples came from: https://www.hostinger.com/tutorials/cron-job.
You can play around with this using the website crontab.guru.
Okay, coming back to our terminal. Let’s create a cron that runs every minute and will update a log file called cron.log
with a simple text. First, we need to create the bash file with the instructions for the job. The filename will be cron.sh
and the content will be:
echo "Hello from cron - $(date +"%Hh%M")" >> <path-to-your-cron.log-file>
This command will add to the end of the cron.log
file a text like this: Hello from cron - 14h22
.
Now we need to configure the cron job to execute the file cron.sh
every minute. Before editing the cron file, first, we need to give execute permission to the cron.sh
file using the command chmod +x cron.sh
. Now, run the command crontab -e
again, go to the end of the file, and write this:
* * * * * <path-to-your-cron.sh-file>
Save the changes and it is done, your cron is configured. Now, let’s check if it is working correctly by monitoring the content of our cron.log
file.
tail -f cron.log
After some minutes our file content should look like this:
Hello from cron - 14h:22
Hello from cron - 14h:23
Hello from cron - 14h:24
Hello from cron - 14h:25
Hello from cron - 14h:26
Conclusion
Congratulations, if you read this far, now you have another topic to discuss at our family dinner. Leaving the jokes aside, today you understand an essential concept of the computing world. Now, start creating your jobs, and you are not limited to writing in bash files, there are many packages, that allow you to run cron jobs on your favorite programming language. I hope that this article has been useful to you. Let me know what you think in the comments.