cron-&-crontab,-explained

If you want to schedule tasks in Linux, you must know all the data of Cron & Crontab.

What are Cron & Crontab in Linux?

Scheduling tasks in the Linux operating system is now easier, and this is due to the use of Cron & Crontrab, but to better understand the subject it is important to know all its details and what does each one mean?

What is Cron?

The word Cron comes from the Greek Chronos, which translates as time, its function in Linux is organize and manage all the tasks or activities that are in the background, but they run processes at very regular intervals. That is, all the actions you want to carry out in a few minutes, hours, weeks or months.

It is very similar to the well-known Windows task scheduler, which is responsible for carrying out an activity in the time that you scheduled, it can be hours, days, weeks, and even months.

How does Chron work?

Cron is also known as the "devil" , and it starts from /etc/rc.d or also as, / Etc / init.d; This will depend on the distribution you have. In addition, it takes place in the background, or what is known as the background.

For this reason, it is in charge of frequently checking all the tasks and activities found in the crontab table. /etc/control, or in the /var/spool/cron, looking for some tasks that have not yet been fulfilled.

The best thing is that you can add your own commands, or scripts so that some processes are carried out automatically. An example of this is when you need to update a system, or when you require backups.

What is Crontab?

It is the text file where all the tasks you schedule in Cron are saved and organized. The main function is review the times and dates at which each command should be executed or the script.

You should know that each of the users has a contrab, and therefore, when it is a root user, reference is made to the /etc/contrab, while in the other case it would be using only the contrab command.

One of its features is that it helps to organize all the tasks with a multi-user system. That is, if you are a root or simple user, you can still use it.

How does Crontab work?

The easiest way to explain how Crotab works is with an example. Automating the task of updating Linux is one of the most frequent.

To start this process you must create a script, it will be known as »cron», it must correctly execute all the instructions assigned to it, but first, it is important that you perform different tests and try in several ways before including it with the cron:

  • #! / Bin / bash
  • #script (update example)
  • #choose distribution
  • #debian-ubuntu
  • #apt-get update&apt-get -y upgrade
  • #fedora
  • #yum -y update
  • #Arch
  • #pacman --noconfirm -Syu

If it is about Ubuntu/Debian it is important that the # is removed, just where the apt-get is located. Then you must save the script with the name of update.sh, and modify the permissions for it to be executed, placing: chmod a + x ~ / scripts / update.sh

You must run the script to check that everything is carried out without any problem. Make sure that there is not a single error, because otherwise there will be failures during the process. And, the last step in this case is to add the task to contrab.

How to add tasks to crontab?

Now, it's time to start running the crontab edit with »contrab-e», and it should look like this: # mh dom mon dow user command. Each of these letters or words has a meaning and we mention it below:

  • m: It refers to the minutes that the script will take to execute, the values ​​vary from to 59.
  • h: It means the hour, the format used is 24 hours, and the values ​​range from 0 to 23.
  • Sun: It is the day of the month.
  • down: It refers to the days of the week, the values ​​go from 0 to 7, where obviously the 7 means Sunday.
  • user: The username that is running the command, either root or something else.
  • command: Refers to the command or path used for the script.

Examples of cron tasks explained

  • 15 10 * * * user/man/user/scripts/update.sh. You should run the update.sh script at 10:15 am every day.
  • 15 22 * ​​* * user/home/user/scripts/update.sh. This example task will perform the script specifically at 22:15 PM every day.
  • 00 10 * * 0 root apt-get -y update User root. Specifically, the script is carried out on Sundays at 10:00 am
  • 45 10 * * sun root apt-get -y update. In this case, the user is root and will execute the update only on Sundays, represented by »sun» at 10:45 am
  • 30 7 20 11 * user /home/user/scripts/update.sh. The script is to be executed on November 20 at 7:30 am
  • 30 7 11 11 sun user /home/user/scripts/cake_with_candles.sh. The task is that on November 11 at 7:30 am, if it is Sunday the user must celebrate the sysadmin.
  • It is also possible to use some special commands, like: 30 17 * * 1,2,3,4,5. Which means that at 5:30 pm from Monday to Friday the script must be executed.
  • 00 12 1,15, 28 * *. It means that at 12 noon the first, fifteen and 28 days of every month must be executed.

However, this is not the only thing that Crontab can do, it also controls some commands that, for many users, are easier than the ones mentioned above, and they are the following:

  • @reboot: It is to execute once at startup.
  • @yearly: You should only run the script once a year 0 0 1 1 *.
  • @annually: It is used in the same way as the previous one.
  • @monthly: It means that the script should only be executed one day a month, specifically the first day 0 0 1 * *.
  • @weekly Weekl: It means the first minute of the first hour of the week 0 0 * * 0».
  • @daily: Runs the script every day at 12:00 am 0 0 * * *.
  • @midnight: It has the same function as the previous one.
  • @hourly: The script should be executed the first minute of every now 0 * * * *.

Manage cron jobs

  • crontab file: It is used to replace a file that already exists in contrab, with another.
  • crontab -e: It is used to edit a contrab file, and with each new line, a new task is set.
  • contrab -I: With this you can create a list of user tasks found in contrab.
  • contrab -d: It works to delete a user's contrab.
  • contrab -c dir: It is to define the directory and crontab of a user. Remember that, it must have all the write and execute permissions of the user.
  • password -u user: It is a prefix used to gain control of another user's contrab: $ sudo crontab -l -u root; $ sudo crontab -e user2; #crontab -d -u user.