In this article we explain how you can schedule these scheduled tasks and many more, through a crontab. A very common task performed with Crontab is to automate backups, system maintenance, and other repetitive tasks. 

What is Crontab?

The crontab command is used on UNIX systems to schedule the execution of other commands, that is, to automate tasks.

It is as simple as a text file. Yes, even if it doesn’t seem like it. What makes it special is its content. Its content specifies a list of all the scripts to be executed by the system . As well as specifying the dates, times and their execution permissions.

On Linux, generally each user has their own crontab file and the one located in the etc directory is owned by the root user.

To generate your own file (in case you are not root user) you just use the command:

Corntab Basic Command

To see them:

sudo crontab -l

To edit them:

sudo crontab -e

Delete the crontab

sudo crontab -d

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

crontab -c dir

Prefix to handle another user’s crontab, examples:

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

Task format

Cron tasks follow a certain syntax. They have 5 asterisks followed by the command to execute. Now I will explain what each thing is for.

m h dom mon dow user command > /dev/null 2>&1

The 5 asterisks

From left to right, the asterisks represent:

  1. Minutes: from 0 to 59.
  2. Hours: from 0 to 23.
  3. Day of the month: from 1 to 31.
  4. Month: from 1 to 12.
  5. Day of the week: from 0 to 6, with 0 being Sunday.

If an asterisk is left, it means “every” minute, hour, day of the month, month or day of the week. For example:

* * * * * /bin/ejecutar/script.sh

Run this script:

  • Each minute
  • Hourly
  • Of every day of the month
  • Every month
  • Of every day of the week

Another example:

30 2 * * 1 /bin/ejecutar/script.sh

Run this script:

  • In minute 30
  • 2 o’clock at night
  • Of every day of the month
  • Every month
  • Only if it’s friday

In short, every Friday at 2:30 am the script will be executed.

Timeslots

Run a script from Monday to Friday at 2:30 a.m.:

30 2 * * 1-5 /bin/ejecutar/script.sh

Run a script from Monday to Friday every 10 minutes from 2:00 a.m. for an hour:

0,10,20,30,40,50 2 * * 1-5 /bin/ejecutar/script.sh

This may be long. The crontab syntax allows the following. Let’s imagine we want to run it every 5 minutes:

*/5 2 * * 1-5 /bin/ejecutar/script.sh

RESERVED WORDS

Many times we have reserved words to facilitate the use of programs or programming languages. Cron could not be less , so we have some that are usually the most common. And each one who configures it according to their needs. Here they go:

  • @reboot: runs only once at startup.
  • @ yearly / @ annually: run every year.
  • @monthly: run once a month.
  • @weekly: once a week.
  • @ daily / @ midnight: once a day.
  • @hourly: cada hora.

For example, to run the script every hour :

@hourly /bin/ejecutar/script.sh

Conclusion

In this article, we have looked at the cron basic command, cron task format, and crontabs. Crontabs are some of the best tools for Unix-like systems to automate repetitive tasks.

Author

Write A Comment