If you’re Linux user surely you have heard about what cron is, this is a tool very similar to Windows program task manager, so its function is to allow the configuration of a program, process or file that is running on the system. In the case of the system Linux operating this is not run from a graphical environment, but from the terminal.
These types of functions are intended to help users to improve your productivity when working in said system, for this it is also important avoid starting repetitive tasks. By having a tool like cron users have the possibility to execute tasks according to the task schedule that has been previously established, offering them greater ease of use.
Therefore, it can be said that cron is a Linux task manager that allows to carry out the command execution on a certain moment, these can be scheduled to run every hour, every day, once a week, among others. To work with cron it will be necessary to do it through crontab command. This is how here we are going to teach you a little more about what this tool is about and how you can get the most out of it.
What is Cron and what is this service of the Linux operating system for?
Cron is a Linux tool that allows you to carry out background processes being this is a very important and common procedure in the system. The execution of the same occurs as soon as start the computer boot and its main function is to be able to take care of all scheduled tasks with a date in specific and automatic and repetitive way. Each of the processes to be executed must be specified in the crontab file.
The operation of all this is very simple, there you must check if they exist pending tasks for execution according to the system schedule. Keep in mind that it is very important to have set the device time correctly, since depending on it, each of the pending activities will be carried out, this means that if the system time is not configured correctly, the results of the activities scheduled with cron will not be as expected.
Finally it should be mentioned that depending on the distribution being used on Linux, can be started using the directories
/etc/init.d o etc/rc,d/ and every minute performs a check of the
/var/spool/cron locating there the possible pending tasks to be executed.
What kind of tasks can be done from Cron? Main applications
It is important to keep in mind that cron tasks is a Linux command whose main function is to periodically execute a script or command indicated by the user. This is how this type of function is used to carry out the periodic or repetitive execution of related scripts with email notifications, routine data checks, database, start programs, among other types of activities available there.
Therefore, in cron you will have the opportunity to execute any action that is repetitive, an example of this so that you can understand it better would be the following: Create a cron task to periodically execute a script that checks the temperature of any weather station, this will let you know the temperature of the area and frequently display it on your website.
When you are creating this task you will have the opportunity to indicate with what how often the script should be run, either each minute, hourly, 10 times a day or just a few days a year. The programming of this activity will depend mainly on the needs of each of the users and the objectives they want to achieve with it.
Learn step by step how to use Cron on Linux like an expert
If you are a new Linux user and you still don’t know very well how to use the cron tool on your computer, so here we are going to show you step by step how you can use it as an expert.
To do this, follow each of the steps that we are going to teach you below:
Keep in mind that cron It is a service that only needs to be started once, mostly starts with the same system boot automatically. East cron service is known as crond and is available in all Linux distributions. It should also be noted that in most Linux distributions this service is installed automatically and is always started together with the system startup.
This can be purchased in the following ways:
#> /etc/rc.d/init.d/crond status
#> /etc/init.d/crond status
Here you can use either one depending on the distro you use.
Now if you have the service command installed on your computer then use the following:
#> service crond status. Crond (pid 507) is running.
Another way to check all this is to check it through the ps command:
If for some reason you have noticed that the cron command is not working or is doing it in a bad way then you can check it as follows:
#> /etc/rc.d/init.d/crond start
If for some reason the cron service is not configured to run with startup from the beginning, you can solve it through the command> chkconfig:
#> chkconfig --level 35 crond on
In this way you will add it to the run level 3 and 5 so that this starts at the time of system boot.
Use Cron from directory etc
Another way to use cron is through the etc directory in two ways, with the first one you will find the following directories:
- Cron daily.
- Cron weekly.
- Cron hourly.
- Cron monthly.
When placing a script file in any of these directories, then the script file will be configured so that each hour, every day, every week, every month, depending on the directory that has been selected.
However, for the file to be executed it must look something like the following:
#!/bin/sh #script que genera un respaldo cd /usr/documentos tar czf * respaldo cp respaldo /otra_directorio/.
As you can see the first line starts with the symbols #!, this indicates that it is a bash shell script, while the other lines are about commands that we want run the script.
It can be called as a backup and change the corresponding permissions so that it can be executed, such as:
#> chmod 700 respaldo.sh #> ls -l respaldo.sh -rwx------ 1 root root 0 Jul 20 09:30 respaldo.sh
As you can see in the owner’s permission group there is a x (rwx) this indicates that it can be executed. If said script file is left with cron hourly, it is going to be running every hour with one minute of every day.
The other way to use cron is by directly manipulating the / etc / crontab file, during its default installation in some of the Linux distributions it will look like this:
#> cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # run-parts 01 * * * * root run-parts /etc/cron.hourly 02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly #> cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # run-parts 01 * * * * root run-parts /etc/cron.hourly 02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly
In the case of the first four lines, they are variables that indicate the following:
Shell: Its about Low shell which is running cron, In the case that this is not specified, then the one indicated in the line will be taken by default
/etc/passwdcorresponding to the user that is executing the cron at that moment.
- Path: It is the one that is in charge of indicating the path of the directories in which cron will look for the command to be executed. It should be mentioned that this path is different from system or user global path.
- Mail to: This is where the command output is sent in case it has an output. Then cron will send an email to whoever is specified in this variable, so it must be a valid user in the system or some other system. Now if this is not specified then cron will send the mail to the owner of the command that is executed.
Home: It is the root or main directory of the cron command, in case this is not indicated, then the root will be the one indicated in the file
/etc/passwdcorresponding to the user running cron.
Commands will always be indicated with the symbol of # at the beginning of the line, this will allow you to differentiate between them. After all of the above you will find the lines that they run the scheduled tasks properly.
Keep in mind that here there are no limits on how many tasks there may be, the fields are usually seven and all these lines are formed as follows:
- Minute: is in charge of controlling the minute of the hour in which the command will be executed, this is a value that must be between the range of 0 and 59.
- Hour: here the time at which the command will be executed will be controlled, for this a 24 hour format and these values must be within a range of 0 and 23 or in that case 0 that it would be midnight.
- Day of the month: You must indicate the day of the month on which the command will be executed, if you want to execute the command on the 15th of the month, then you must place 15.
- Month: In the case that you want the command to be executed in a specific month, then here you must select it, for this it can be indicated by a number that it must go between 1 and 12 or by the name of the month in English or only the first three letters of it.
- Weekday: If it is going to be executed one day a week, then you can indicate it with a numeric value between 0 and 7 or by the name of the day in English or just put the first three letters of the day.
- User: The user who is executing the command.
- Command: command, program or script that you want to execute, this is a field that can contain multiple spaces or words.
In this way you can start to specify when you want run a specific task.
Use Cron with Crontab
In the case that you want run cron on multiple users, then it will be necessary to do it with
crontab, for this we must bear in mind that Linux it’s a multi-user operating system and cron is a service that has been created for it, so it will be able to develop very well. In this case, each of the users can count on their own own crontab file, in fact the file
etc/crontab it is assumed to be the root user’s file, but nothing happens if other users are included.
In this way it will also be indicated who is the user who is executing the task and it is mandatory in
/etc/crontab. But in the case that users have to generate their own crontab file, then it will be necessary to use the crontab command. In the directory of
/var/spool/cron it may vary depending on Linux distribution that you use, a cron file will be generated for each of the users, although the file is text, it should not be edited directly.
Therefore, there are only two situations to be able to directly generate the crontab file with the command:
With this you can open the editor and with the file called crontab empty will be where the user enters their task table and it is automatically saved as
/var/spool/cron/usuario. The other possible case is that the user is in charge of editing a normal text file but with the inputs of the tasks as it can be “micron” later the command
$> crontab mi_cron, he will be responsible for establishing it as his user’s cron file in
Let’s see next:
$> vi mi_cron # borra archivos de carpeta compartida 0 20 * * * rm -f /home/sergio/compartidos/* # ejecuta un script que realiza un respaldo de la carpeta documentos el primer día de cada mes 0 22 1 * * /home/sergio/respaldomensual.sh # cada 5 horas de lun a vie, se asegura que los permisos sean los correctos en mi home 1 *5 * * * 1-5 chmod -R 640 /home/sergio/* :wq (se guarda el archivo) $> ls mi_cron $> crontab mi_cron (se establece en /var/spool/cron/usuario)
Taking into account all the aforementioned and considering other contrab options we have the following:
$> crontab archivo.cron (establecerá el archivo.cron como el crontab del usuario) $> crontab -e (abrirá el editor preestablecido donde se podrá crear o editar el archivo crontab) $> crontab -l (lista el crontab actual del usuario, sus tareas de cron) $> crontab -r (elimina el crontab actual del usuario)
In some of Linux operating system distributions when edited the crontabs of normal users it is necessary to apply a restart to the service so that it can reread all the files crontab in
If you have any questions, leave them in the comments, we will answer you as soon as possible, and it will surely be of great help to more members of the community. Thanks! 😉