Back up every night, send a report every Monday morning, clean temporary files every hour. Such repeated tasks do not have to be done by a person, cron takes care of them. Let us explain what cron is, how it is used and show a concrete example.

What cron is

Cron is a task scheduler in Linux and similar systems that runs commands automatically at a set time. It runs in the background and at regular intervals checks whether it has something to run. A single scheduled task is called a cron job.

Thanks to cron, repeated things happen on their own, precisely and reliably, without anyone having to think about them.

What cron is used for

  • Backing up databases and files at night, this relates to the article on how to back up correctly.
  • Regular reports and emails.
  • Cleaning up temporary files and logs.
  • Syncing data between systems.
  • Maintenance scripts and checks.

It is the foundation of automation on servers and in self-hosting.

How cron is written

Tasks are written into the so-called crontab. Each line has five time fields and then the command to run:

FieldMeaningRange
1minute0 to 59
2hour0 to 23
3day of month1 to 31
4month1 to 12
5day of week0 to 6 (0 = Sunday)

An asterisk * means “every” (every minute, every hour and so on).

A practical example

Let us say we want to run a backup script every day at 2:30 in the morning:

30 2 * * * /home/backup/backup.sh

We read it like this: at minute 30, at hour 2, every day, every month, every day of week, run /home/backup/backup.sh.

More examples:

  • 0 * * * * means every full hour.
  • */15 * * * * means every 15 minutes.
  • 0 8 * * 1 means every Monday at 8:00.
  • 0 0 1 * * means the first of the month at midnight.

You edit the list of tasks with the crontab -e command, which relates to working in the terminal and to the basic Linux commands.

What to watch for

  • The correct time and time zone of the server, so the task runs when you expect.
  • Full paths to files, cron does not have the same environment as when you are logged in.
  • Logging the result, so you know whether the task ran and succeeded.
  • Checking that the task really runs. A silent cron that fails is dangerous, because you might not notice.

The Windows alternative

In Windows the same job is done by the Task Scheduler. It works on a similar principle, just through a graphical interface: you choose what and when to run.

Conclusion

Cron is a quiet helper that reliably does repeated tasks for you at a set time. Just understand the five time fields and you can schedule backups, reports and maintenance. The important thing is to set the tasks up correctly and check that they really run.

Need to automate backups, maintenance or tasks on a server? Get in touch, we will set it up reliably and with monitoring.

This article is part of our Software and system overview.