Scheduling and automation are fundamental aspects for what it is the administration of computer systems, that is why power schedule tasks on your Linux system is very important for further improve its performance. For this there are some commands that will allow you to do it in a very simple and fast way.
This is how here we are going to teach you a little more about what it is the Cron command, which is considered as one of best time-based task scheduling tools and that is available for Unix-like operating systems, as well as for some of Linux distributions.
Therefore, when wanting to carry out this type of activity it is very important resort to these types of tools, which are run in the background and automatically, which makes its operation of task automation much more effective. In accordance with all this, here we are going to teach you a little more about how to schedule tasks in Linux via the Cron command, To do this, follow in detail everything that we will teach you below.
What should I keep in mind before programming a process in Linux?
The programming processes They are not as simple as many may think, for this you need to have a little computer knowledge that will help you make this process much easier to assimilate and perform. In the case of Linux has the Cron tool which allows to carry out task schedules and automations in the operating system.
These types of tools are very important, so you have to know how to take advantage of them, they will allow you automatically execute a script or a PHP file from time to time, or any other computer element you want. This will avoid having to access as many times a day as necessary to be able to activate it manually, so it wouldn’t make much sense.
Keep in mind that when these types of programming are carried out in Linux they allow you to develop and facilitate certain types of tasks such as the following:
- Set up update tasks automatically.
- Set up a backup automatic.
- Make some scheduled tasks in a linux server.
- Set up a task that I sent a mailing so that this does not have to be done manual way, but rather automatically.
What are the most recommended commands for scheduling tasks in Linux?
In the case of Linux operating system can be found two commands highly recommended for what it is scheduling tasks in the OS, these are the aforementioned Cron and Crontab. For many, both tools can look like one, and they are very similar in terms of operation, so they could be considered as two elements dependent on each other.
However, to make all this much clearer, here we are going to show you what each of these Linux commands available to carry out what is the programming of tasks within the system consists of:
This tool is considered as a daemon when scheduling tasks in Linux, he himself usually acts in background so it runs at the same time the operating system starts. This begins to check if there is any task that must be executed according to the time that has been configured in the system.
How these tasks go from hand with the time, since they must be programs so that run automatically every few hours, then it is essential that the time zone of the equipment is very well configured, of what contrary Cron executions may end up developing in the hours that are not correct, since the schedule of equipment will not match what is adjusted in reality.
This function is usually started using the folders /etc/rc.d/ or /etc/int.d where every 60 seconds it checks the files / etc / crontab or / var / spool / cron, all this is done by looking for possible execution of pending tasks.
Crontab is considered as a Text fileAs simple as it may seem, this is the main definition of this command. But the truth of all this is that Crontab is a file that contains special information, in it you can find a list of all the scripts to be executed.
Usually each of OS users have their own Crontab file, so it is considered that the one located in the etc folder belongs to the root user. So that you can create your own file, it will be necessary for the user to use the Crontab command, so it is called the same way.
Learn step by step how to program repetitive processes in Linux with cron
Having completely clear what it is repetitive process schedules in Linux, and knowing the two main command that allow to carry out these processes, the next thing will be to teach you step by step as you can schedule tasks in the OS.
To do this, follow in detail everything that we will teach you below:
Schedule tasks in Linux with Cron
In order to start this procedure through the Cron command the first thing you should do is run the following command according the user you want to modify.
Let’s see below:
- To change the cron of the user you are connected to: “Crontab -e” these codes must be insert without quotation marks (“”).
- Modify the cron of a specific user: “Crontab -eu user”.
When any of the the two codes already mentioned, the following will be to fill them, for this you must place the first five numbers, where they must be specified aspects such as the day, the hour, the days of the week, among others.
To do this, you must do it as follows:
- In this case the first number will indicate the minute.
- The second number will tell you the time.
- The third number It will be the day.
- The fourth number will be the month.
- And finally the fifth day will indicate the day of the week.
In this way, you must fill it as they are your needs.
So that you can understand a little better, here we explain what each of the numbers means:
- 0 sunday
- 1 Monday
- 2 Tuesday
- 3 wednesday
- 4 Thursday
- 5 friday
- 6th Saturday
After adding each of the numbers must be separated with a space. After you have configured all this to your tastes and needs, the following will be select the script to run.
So that all this can be understood in a better way, here are some examples of configuration scripts with Cron:
- 02 * * * script_to_run.pl
- According to the mentioned command it is meant that in at minute 0, at 2, every day of the month, every month, every day of the week, you want to run the script.
If what you want is run a .PHP, then what is owed is configure the days and time of the execution, to later be able indicate the installation path of the PHP and together with the space the PHP to be executed.
For this case the following command must be placed:
- 0 0 * 12,1 * / usr / bin / php /path/del/file/file.php
- So the above command will state the following: en At minute 0, at 0 o’clock, every day, during months 12 and 1, every day of the week you want to execute the following .PHP
If you wish, you can also place it in the following way so that it is run every x hours or minute as the case may be.
For this you can place it in the following way:
- 0 * / 3 * * * run.pl (this would run every 3 hours)
- * / 90 * * * * run.pl (this runs every 1 hour 30 minutes with the * / 90 (90 minutes = 1:30))
This way you can start configure cron command to bring about the process of scheduling tasks on the Linux system, an easy way to perform repetitive processes automatically without the need to develop them manually.
Add tasks to the Crontab command
As mentioned above in the post, Crontab is the other most recommended command to carry out task scheduling in this OS, That is why here we are going to explain how you can start using it easily and quickly. At the time of execute the edit of the crontab with crontab –e. in some of the Linux distributions as it happens in Ubuntu offers the option of choosing the desired text editor.
So the Crontab file can be seen as follows:
- # mh dom mon dow user command
Where each of these values means the following:
- m: In this case the letter m corresponds at the minute the script was executed, the value of this ranges from a range from 0 to 59.
- h: It is used to define the exact time, in this case a 24 hour format, values range from 0 to 23, being 0 12:00 midnight.
- Sun: This term makes reference to the day of the month, here you can specify 15 if you want the script runs every 15 days.
- dow: It allows you to specify the day of the week, this value can range from 0 and 7, or in that case the first three letters of the day, but these must be in English.
- user: Allows you to define the user with whom you will execute the command, this may be a root or another different user, the important thing here is that said user has the permission to running the script.
- Command: Refers to the absolute path of the script to be executed, an example of this is the following: /home/usuarios/scripts/actualizar.sh. if this path corresponds to a script, then it will run correctly.
So that all this can be understood in a better way, here are some examples of various programmable tasks:
- 15 22 * * * users /home/usuario/scripts/actualizar.sh
- In this case, run the script at 10:15 pm every day.
- 15 10 * * * user /home/usuario/scripts/actualizar.sh
- Allows you to run a script at 10:15 am every day.
- 00 10 * * 0 root apt –get –y update root user
- Used to run a update every Sunday at 10:00 am
- 30 7 20 11 * user /home/usuario/scripts/actualizar.sh
- Indicates that the day November 20 at 7:30 p.m.m. the user will run the script.
- 30 7 11 11 sun user /home/usuario/scripts/pastel_con_velitas.sh
- Indicates that the November 11 at 7:30 am. and let it be Sunday the user will celebrate his sysadmin.
- 45 10 * * sun root apt-get –y update
- In this case the root user executes a update every Sunday at 10:45 am
- 01 * * * * user /home/usuario/scripts/molestorecordatorio.sh
- It is a annoying reminder every minute of every hour every day, usually it is not recommended to use as it turns out to be very tedious.
- 00 12 1,15,28 * *
- It will tell you that 12 pm every 1st, 15th and 28th of each month, is widely used for business payroll.
- 30 17 * * 1,2,3,4,5
- This means that a 5:30 pm every day from Monday to Friday the reminder will be generated.
If you have any questions, leave them in the comments, we will answer you as soon as possible, and it will also be of great help to more members of the community. Thank you! 😉