The use of cron for lazy. Linux and deadly sins part two

sleeping tiger

If you're lazy to do repetitive tasks on your computer, Cron does them for you.

This is the second article from a series in which we use the list of what the Catholic Church calls "deadly sins" as an excuse to cKnow more about commands and programs of the Linux world. In this case we continue talking about the use of cron that will be very useful for those who cultivate laziness.

Given that we are living in a time of people who are easy to offend, let me clarify, before getting into the matter, that it is not my intention to make fun of anyone's religious beliefs. In any case, it's a self mockery. I spent a large part of the XNUMXs studying catechism so that after having taken my First Communion I never set foot in a church again unless some family event required it. I have to get that time back somehow.

What are cron and crontab for?

We had said that cron is a daemon, that is a program that runs in the background without user intervention. Looking for additional information for this article, I discovered that translating daemon (The way Unix systems and derivatives call this type of program) as daemon is a widespread errorbut I'm not going to correct it. We are talking about sins, there has to be at least one demon.

The function of cron is to execute, at a previously specified time, a certain task. Most of the time it is due to system needs, although users can indicate others by editing a text file known as crontab.

In the previous post we had said that the commands to create crontab are:

crontab –e for the default user

O

crontab –u nombre_de_usuario for any of the others.

Crontab is a text file that gives Cron instructions on what to do and when to do it.

About using cron via crontab

To create our crontab we must take into account the following:

  • One line is used for each task.
  • The date and time of execution of the task must be indicated. In case it is a task that requires periodicity. For example, every Wednesday at 5 am, the rest of the parameters are replaced by asterisks (*).
  • If you want to assign more than one value for a given parameter, each value must be separated by a comma.
  • Parameters are separated with a space.
  • The directory where the command launcher is must be known

For example, if we want our children's computer to turn off every day at 20:XNUMX p.m., the instruction would be

0 20 * * * /sbin/shutdown

In case we want the shutdown to be only on Sundays, we change the instruction to

0 20 * * 0 /sbin/shutdown

There are some shortcuts that save us from having to type all the parameters. They are:

  • @hourly: Execute a command at the hour o'clock. 
  • @daily: Run the command at the beginning of each day.
  • @weekly: Run the command at the beginning of the first day of the week.
  • @monthly: Run the command at the beginning of the first day of every month.
  • @yearly: Execute the command in the first minute of the year.

Some examples of using this command are:

@daily /bin/sh /ruta_al_script/nombre_del_script.sh run a Bash script.

@hourly /bin/python3 /ruta_al_script/nombre_del_script.py run a python script every hour.

In all cases scripts must have execute permissions.

In the examples we saw, not only the command is indicated, but also the path where the executable is located. We are going to work with these directories:

  • /bin: It contains all the applications necessary for the operation of the system.
  • /sbin: Here are the applications that the root user needs to manage the system.
  • / Home: Where the applications of each user are stored.
  • /usr: Applications and files installed by users are stored here. They include files with the names and functions mentioned in this list.

In the next article we will see how to improve crontab writing and some other automation tools available for Linux.


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.