Cron & crontab, explained

Lucaine published a while ago excellent tutorial on cron and crontab that I think is worth sharing. Cron is a kind of equivalent to Scheduled Tasks in Windows, only that it is handled from the terminal. Those who prefer a visual interface to achieve the same goal, can see this Another item.

What is cron?

The name cron comes from the Greek chronos which means "time". In the Unix operating system, cron is a regular background process manager (daemon) that runs processes or scripts at regular intervals (for example, every minute, day, week, or month). The processes that must be executed and the time at which they must be executed are specified in the crontab file.

How it works

The cron daemon starts from /etc/rc.d/ o / Etc / init.d depending on the distribution. Cron runs in the background, checks the crontab task table every minute / etc / crontab or in / var / spool / cron in search of tasks to be accomplished. As a user we can add commands or scripts with tasks to cron to automate some processes. This is useful for example to automate the update of a system or a good backup system.

Related article:
Tutorial: Install .tar.gz and .tar.bz2 Packages

What is Crontab?

Crontab is a simple text file that stores a list of commands to be executed at a time specified by the user. Crontab will check the date and time when the script or command should be executed, the execution permissions and it will do it in the background. Each user can have their own crontab file, in fact the / etc / crontab it is assumed to be the root user's crontab file, when normal users (and even root) want to generate their own crontab file then we will use the crontab command.

Crontab is the easiest way to manage cron tasks on multi-user systems, either as a simple system user or root user.

Using crontab

We are starting with a simple example.

We are going to automate the update of a system, to eliminate the annoying of "I always have to update and I don't like that!"

how to
Related article:
Commands to know the system (identify hardware and some software configurations)

First of all we will make a script. This script will be called by cron and will contain all the instructions we want it to do, therefore it is necessary to test it in several cases and in several ways before including it in cron, a simple update script like this:

#! / bin / bash #script update example #choose your distribution # debian-ubuntu # apt-get update & apt-get -y upgrade #fedora #yum -y update #Arch #pacman --noconfirm -Syu

Remove the # from your distro line. In case it's Ubuntu / Debian, it starts with apt-get.

We save the script as update.sh (eg scripts directory your home). We change the execution permissions of the said script with:

chmod a + x ~ / scripts / update.sh

We execute the script a couple of times to verify that everything runs smoothly, we modify what is necessary (it must not contain errors, otherwise cron will only repeat an error over and over again). Now to add the task to our crontab.

Add tasks to crontab

We execute the edition of the crontab with crontab -e, in some distros (such as Ubuntu) it gives us the option to choose the text editor we want, the rest we are left with vi. The crontab file will look something like this.

# mh dom mon dow user command

where:

  • m corresponds to the minute the script will be executed, the value ranges from 0 to 59
  • h the exact time, the 24-hour format is handled, the values ​​range from 0 to 23, with 0 being 12:00 midnight.
  • sun refers to the day of the month, for example you can specify 15 if you want to run every 15 days
  • dow means the day of the week, it can be numeric (0 to 7, where 0 and 7 are Sunday) or the first 3 letters of the day in English: mon, tue, wed, thu, fri, sat, sun.
  • user defines the user who will execute the command, it can be root, or a different user as long as they have permissions to execute the script.
  • command refers to the command or the absolute path of the script to be executed, example: /home/usuario/scripts/update.sh, if it does call a script it must be executable

To be clear a few examples of cron tasks explained:

15 10 * * * user /home/usuario/scripts/update.sh

It will run the update.sh script at 10:15 am every day

15 22 * * * user /home/usuario/scripts/update.sh

It will run the update.sh script at 10:15 pm every day

00 10 * * 0 root apt-get -y update Root user

It will run an update every Sunday at 10:00 am

45 10 * * sun root apt-get -y update

Root user will run an update every Sunday (Sun) at 10:45 am

30 7 20 11 * user /home/usuario/scripts/updata.sh

On November 20 at 7:30 the user will run the script

30 7 11 11 sun user /home/usuario/scripts/pastel_con_velitas.sh

On November 11 at 7:30 am and that is Sunday, the user will celebrate his sysadmin (that is, me)

01 * * * * user /home/user/scripts/molestorecordatorio.sh

An annoying reminder every minute of every hour every day (NOT recommended).

They can still be handled special ranges:

30 17 * * 1,2,3,4,5

At 5:30 in the afternoon every day from Monday to Friday.

00 12 1,15,28 * *

At 12 noon every first, fifteenth and 28th of each month (ideal for payroll)

If this is confusing, crontab handles special strings to define these ranges.

@reboot Run once, at startup
@yearly runs only once a year: 0 0 1 1 *
@annually same as @yearly
@monthly runs once a month, the first day: 0 0 1 * *
@weekly Weekly the first minute of the first hour of the week. 0 0 * * 0 ″.
@daily daily, at 12:00 A.M. 0 0 * * *
@midnight same as @daily
@hourly at the first minute of every hour: 0 * * * *

Its use is very simple.

@hourly user /home/user/scripts/molestorecordatorio.sh @monthly user /home/user/scripts/backup.sh @daily root apt-get update && apt-get -y upgrade

Last but not least:

Cron job management

crontab file

Replace the existing crontab file with a user-defined file

crontab -e

Edit the user's crontab file, each new line will be a new crontab task.

Crontab -l

List all the user's crontab tasks

crontab -d

Delete the user's crontab

Crontab -C dir

Defines the user's crontab directory (this must have the user's write and execute permissions)

crontab -u user

prefix to handle another user's crontab, examples:

$ sudo crontab -l -u root $ sudo crontab -e user2 #crontab -d -u user

This tool, like many others, can be seen in more depth and in more detail in:

Thanks Lucain!