About cron and anachron for lazy people. Linux and deadly sins part XNUMX

Empty crontab file

This crontab file is used to tell cron what statements to execute.

Here at the third part of this series of articles in which we are using the seven deadly sins as an excuse to know tools and programs for Linux, we will comment more on the cron configuration and we will talk about an alternative

cron is a stand-alone program to run system maintenance tasks on a regular basis. Tasks are specified in a text file known as a crontab.

About cron and anachron

In the previous articles we discussed the structure of the instructions to give to cron. Now we will see an easy way to write the crontab file.

The command crontab –e generates an almost blank crontab file. There are only a few lines preceded by the pound sign (#). This sign indicates that they are comments and should not be attempted to be executed. We can delete them or leave them.

We should start by adding these lines:

SHELL=/bin/bash: Specifies that the shell to use to run the commands is Bash. A shell is a command interpreter that allows the user to access system resources.

MAILTO=una_dirección_de_mail: In server-side distributions it is common to specify an email address for administrators to receive bug reports. That is why it is the option that cron uses by default to send them. On desktop computers that address is not configured so we can do it with this variable.

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin: This saves us from having to specify the path to the executable for each command.

anachron

Cron is the best known of the automation tools, but it has the drawback that, if the computer is turned off or suspended, the task is not performed until it returns to the preset date. Anacron. On the contrary, it takes care that when the session is resumed, the pending tasks are executed. The program finds out when an instruction was last executed and how often it was told it should be executed. The list of pending jobs is recorded in a text file called in this case anacrontab. Unlike cron and crontab, the date is set by setting a day, a delay in minutes, a job identifier and the command to execute.

For each job, Anacron checks if this job has been executed in the last n days, where n is the period specified for that job. If not, Anacron runs the job's shell command, after waiting the number of minutes specified as the delay parameter.

After the command completes, Anacron logs the date to a special timestamp file for that job, so you know when to run it again. Only the date is used for time calculations. Time is not used.

We can find anacron under this name or as part of another package called cronie in the repositories of the main Linux distributions.

Anacron Parameters

-F: Executes the task regardless of whether it is the indicated date.

-you: Changes the date of the jobs to the current one, but does not execute them.

-s: Sets that a task will only be started when the previous one is finished.

on: Run the job no matter what the date is. Turn on the -s parameter.

-d: It does not run in the background and errors are generated as a standard error message. The result of the work is sent by mail. In the case of a desktop system, the email address is configured as indicated above.

-q: Prevents standard error messages from being generated. It is used together with -d.

-t: In case you want to use a different task list than the one created by default, this parameter tells anacron which one to use.

-T: Test the task list to see if it is configured correctly. If it is not, it generates an error message.

-S Store timestamps in a specified directory for use by anacron.

In the next article we will see how to correctly configure anacrontab


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

Your email address will not be published.

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.