It’s essential to create a predictable schedule for events on computers. It’s crucial because, as humans, we occasionally struggle to reliably remember to do things when we become distracted, have a lot on our minds, or are on vacation. Computers are excellent at following a schedule, despite the fact that humans must program them before they can do anything.

In some ways, the cron system serves as a simple and basic introduction to programming. By simply editing a file, you can instruct your computer to perform a specific task. The location of the file need not even be known to you. You only need to enter a single command, the “recipe” you want your computer to follow, and hit “save” to finish your work. Your computer will then begin to carry out your instructions at the appointed time until you tell it to stop.

Cron is not intended to be a complex system. Here are the details you need to be aware of.

What is cron?

The cron command is so commonplace in Linux and Unix, and it’s been imitated and reinvented so frequently, that it’s practically become a generic term for anything that occurs on a schedule. It is a type of automation, and even though there are numerous implementations (Dillon’s cron, Vixie’s cron, chrony, and others), as well as variations like anacron and system timers, the syntax and workflow have essentially not changed over the years.

Cron utilizes a “spool” system, just like email and printers. The idea behind a spool file is that you shouldn’t give it much thought, so it’s okay if you weren’t aware that printers and email use them. The directory /var/spool on a Linux system is intended to serve as a central repository for crucial but low-level files that users aren’t intended to directly interact with. Cron tables, also referred to as “crontab,” are one of the spools managed in /var/spool.

A Linux system has a crontab for each user, including you. Each user’s own crontab can be modified, viewed, and removed. Additionally, users can schedule tasks using their crontab. Every job listed in a crontab is executed at the time specified by the crontab, which is monitored by the cron system itself.

Edit cron settings

The crontab command and the -e (for edit) option can be used to modify your crontab. The vim text editor typically opens by default on computers. You can select a different editor in your /.bashrc file if, like me, you don’t use Vim. Instead of using Emacs, which is what Wedid, you could use Nano, Kate, or any other editor you prefer. Your terminal’s text editor is controlled by the environment variable EDITOR, while your graphical editor is controlled by the environment variable VISUAL:

export EDITOR=nano

export VISUAL=kate

Using your modified configurations, restart your shell session:

$ source ~/.bashrc

Make the following modifications to your crontab using your preferred editor:

$ crontab -e

Create a task

In its most basic form, the cron system functions as a calendar. Five different attributes—minute, hour, date, month, and weekday—can be used to specify how frequently cron should execute a job. You can think of these attributes as filters or masks despite the strict and occasionally illogical order in which they must be listed. You might assume that everything is pre-configured to always or every. Every minute during every hour of every day throughout the entire year, this entry would execute touch /tmp/hello:

* * * * * touch /tmp/hello

Setting specific definitions for each attribute will allow you to limit this exhaustive schedule. Set the minutes to 30 to have the job run every hour at the half-hour mark:

30 * * * * touch /tmp/hello

This instruction can also be limited to a particular time. Every day at 3:30 AM, this job starts:

30 3 * * * touch /tmp/hello

A second option is to limit the job’s operation to the first of each month:

30 3 1 * * touch /tmp/hello

From 1 for January to 12 for December, you can set a month, and from 0 for Sunday to 6 for Saturday, you can set a day. In April, only on Mondays, this job is scheduled to start at 3:15:

15 3 * 4 1 touch /tmp/hello

Set increments

These options perfectly match a value in each case. Using cron notation, you can schedule tasks to run at predetermined intervals. An example of a job you could run every 15 minutes is:

*/15 * * * * touch /tmp/hello

Every third day at 10 AM, you could run the following job:

* 10 */3 * * touch /tmp/hello

Every six hours, you could perform a task:

* */6 * * * touch /tmp/hello

Cron shorthand

Modern cron implementations now include a useful shorthand for typical schedules. Which are:

  • @hourly
  • @daily
  • @weekly
  • @monthly
  • @yearly or @annually

List cron jobs

You can see a list of your scheduled cron jobs by using the crontab command:

$ crontab -l

15 3 * 4 1 touch /tmp/hello

Deleting a crontab

The -r option can be used to remove a crontab once you’re done with it: $ crontab -r -i

Interactive refers to the -ioption. Before deleting the file, it requests your confirmation.

Capacities of cron

Understanding how to use cron is one thing, but understanding when to use it is quite another. A good backup plan is the standard use case. You can schedule a regular backup of an important partition if your computer is running continuously or for the majority of the day. Every day at 3AM, we run a backup program called rdiff-backup on our primary data partition:

$ crontab -l ** ** grep rdiff

__ 3 __ __ __ rdiff-backup /data/ /vault/

System maintenance is another typical application. We update our local repository catalog on the Slackware desktop every Friday in the afternoon:

$ crontab -l ** ** grep slack

__ 14 __ * 5 sudo slackpkg update

To clean up my Downloads folder, We could also run the following Ansible script every three days at 15:30:

$ crontab -l ** ** grep ansible

__ 15 **/**3 ____ __ ansible-playbook /home/seth/Ansible/cleanup.yaml

Your computing environment benefits greatly from a small investment in its health. De-duplication scripts, /tmp directory and file size monitors, photo resizers, file movers, and a plethora of other tedious tasks can all be scheduled to run in the background and keep your system clean. Your computer can care for itself in ways with cron that we only wish our actual apartment could.

Observe the cron settings

In my experience, remembering the syntax of cron has been more difficult than coming up with a reason why you need it. When you have it ingrained in your memory, say this to yourself repeatedly:

Minutes, hours, date, month, weekday.

Minutes, hours, date, month, weekday.

Minutes, hours, date, month, weekday.