In order to managing repetitive tasks requires scheduling processes with a temporal frequency. For this the cron command is used, which is helped by a file called Crontab.
In this file the way in which the processes must be managed is established for each user. But this is not all you need to know about Crontab, since it has other secrets that you should keep in mind.
If you want learn more about Crontab, we invite you to continue reading this post. You are going to know what type of tasks can be scheduled and the steps you must do in the Linux Operating System.
What are Crontab commands and what are they for in Linux?
Before knowing how the Crontab command works it is necessary to know what another command is for, the cron
. The latter is a program that is used, inside a Linux terminal, every time you need to run processes in background in a precise moment. This means that the action will take place (for example) every minute, every day, every certain number of hours or once a week.
To specify how it should work cron it is necessary to write the instructions in a specific file, called crontab. This element is a text file that is saved within the command and that can be individualized and personalized by each user. From all this it follows that Crontab is a file that is saved within the cron command to specify the instructions that are necessary to carry out the task of the main command.
What kinds of tasks can be scheduled using the Crontab commands in Linux?
Among the tasks that can be scheduled using Crontab in Linux are:
- Activate a notification in order to remember events at a certain time.
- Update programs establishing the day and time in which you want this process to run.
- Start your Linux antivirus in the background at a specific time.
- Delete files that they are on shared folders.
- Customize permissions of user every certain period.
- Save a file at an exact time of day.
- Ask users to access password once a week.
Learn step by step how to use Crontab to automate tasks in Linux
The step by step you must do to use Crontab correctly and thus automate tasks in Linux is the following:
Start Crontab
The first thing you will have to do is create a script, which will work with cron and thus can follow the instructions you want the command to perform at a certain time. We will take as an example that you need the operating system update to be done automatically.
For this you will have to enter the console and write:
After what you have created this script you will have to save it as actualizacion.sh
Y change execute permissions by typing chmod a+x ~/scripts/actualizacion.sh
.
Add tasks
What you will have to do now is include what you want done from time to time (In our example it is the OS update, but you can also run the antivirus, check email, etc). To add a task you will have to use argument -e
, which will help you choose a text editor.
This will allow the Crontab file to be:
# m h dom mon dow user command
In this way you will have to enter the corresponding minutes to run the script (m), The exact time (h), the day of the month the task will be performed (Sun) and the day of the week (dow, it can also be numeric by writing 1 like Sunday). For more information, look carefully at the image in this post, it will help you understand all the variables. You will also have to write the username (user)
and the path to access the script (command)
.
Examples of this are:
- In order to update every day at 11.30 am the script you will have to write
30 11 * * * usuario /home/usuario/scripts/actualizar.sh
. - In case of want to run it on November 20 at 6.30 p.m. you will have to write
30 18 11 20 sun usuario /home/usuario/scripts/actualizar.sh
.