The cron and crontab command in Linux: schedule regular tasks

Introduction

In Unix operating systems, automating repetitive tasks is essential to save time and reduce human errors. The devilcronallows to run commands or scripts at predefined times, whilecrontabis the interface each user uses to define their own schedule table. In this article we will see how cron, crontab syntax and several practical examples you can apply immediately on your server or workstation.

What's cron?

Cron is a background process that runs continuously and reviews, every minute, if there is any scheduled input that matches the current time. When you find a match, you launch the command associated with the privileges of the user who programmed it. Its design is simple but powerful: it does not require graphic interfaces and works in virtually any Linux distribution.

How does crontab work?

Each user has its own filecrontab(cron table) that is stored in/var/spool/cron/crontabsor, in more modern systems, is managed by commandcrontab -e. When editing this file, a list of lines is defined where each line represents a task and consists of five time fields followed by the command to run.

Crontab syntax

The basic structure is:

* * * * * command _ a _ run - - - -| | | | || | | | + --- day of the week (0-7, where 0 and 7 are Sunday)| | | + ----- month (1-12)| | + --- Day of the month (1-31)| + ----- time (0-23) + --- minute (0-59)

The asterisks indicate «all values». In addition, you can use ranges, lists and steps. For example,*/15in the minute range it means every 15 minutes;10-15indicates a range; and1,3,5specifies a list of values.

Practical examples

  • Support a database every day at 02: 30: 30 2 * * * /usr/bin/mysqldump -u root -pMiClave mi_base > /backups/db_$(date +\%F).sql
  • Clean older temporary files of 7 days: 0 3 * * * find /tmp -type f -mtime +7 -delete
  • Reset a web service every Monday at 04: 00: 0 4 * * 1 systemctl restart apache2
  • Send a record use report every Friday at 09: 15: 15 9 * * 5 df -h | mail -s 'Uso de disco' admin@ejemplo.com
  • Run a custom script every 10 minutes: */10 * * * * /home/usuario/scripts/revision.sh
  • Update the package index and apply security updates every Sunday at 05: 00: 0 5 * * 0 apt-get update && apt-get upgrade -y -o Dpkg::Options::='--force-confold' >> /var/log/auto-update.log 2>&1
  • Rotate logs of a personal application every hour: 0 * * * * /usr/sbin/logrotate /etc/myapp/logrotate.conf

Good practices and treatment

To avoid surprises, follow these tips:

  • Use absolute routes in the commands; the cron environment is minimal and may not have the same PATH variables as your interactive shell.
  • Redirect the standard and error output to a log file to review what happened:comando >> /var/log/micron.log 2>&1
  • First test the command in the terminal before programming it; check that it works without human intervention.
  • If a task does not run, check the cron demon log (usually/var/log/syslogor/var/log/cron) and the user's mail, as cron sends the output by mail if it is not redirected.
  • Keep the crontab entries organized and comment on each line with your purpose using the symbol#.
  • Avoid placing tasks that consume many resources in high-load periods; useniceorioniceto adjust the priority if necessary.
  • Check regularly the crontabs of all users withcrontab -l -u usuarioto detect obsolete or dangerous inputs.

Conclusion

Cron and crontab are key tools for any Linux system manager. Dominating your syntax and applying good practices will allow you to automate backup, cleaning, reporting and any repetitive task, releasing time for more valuable activities. It begins to experiment with small programs and, gradually, builds a more efficient and reliable environment.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish