Performing the same function repeatedly can exhaust a person quite quickly. That's why people seek out new and improved ways to save precious time and automate processes. Cron is a solution that does just that and more - keep reading to find out how.
What is cron?
Cron is a process scheduler that allows you to execute commands, scripts, and programs following specified schedules. The name simply originates from Chronos, the personification of time in Greek mythology; not to be confused with the titan Cronus, the father of Zeus. The two have been commonly mixed-up and the spelling of cron probably won’t help for another millenia or two. As for the technicalities, Cron is a clock daemon (but have no fear, there’s nothing demonic about it) or a service for UNIX-based operating systems such as Linux and although customarily used for automating system maintenance and administration (i.e. monitoring disk space, automating backups), cron has many other uses: downloading software updates, sending reports and invoices, automatically processing data, and many others.
What is a cron job?
The commands that are executed using cron are called ‘cron jobs’. Each cron job consists of a cron expression and a command. The cron expression informs cron when to run the command and is easy for the computer to read, which isn't the case for us mortals, although it is doable.
Each field can contain a single value (e.g. 1), a list of values (e.g. 1,2,3), a range of values (e.g. 10-20), or a range with steps (e.g. 10-30/5). If you’d like to learn more, check out our complete guide to Cron expressions. If you would like guided assistance, check out Cron Expression To Go.
The command portion can be as simple as running a single command, but it can also be a crazy one-liner bash script - that’s up to you to decide. Trivial use cases can include running nightly backups, archiving data on Sunday mornings, removing stale sessions from the database every 5 minutes, and with smart homes, cheap commodity computers, and controllers like Raspberry Pi and Arduino, the sky's the limit. People are using cron to automate anything that needs to run on schedule - from alarm clocks to switching on or off lights and air conditioning.
What is a crontab?
The tab in crontab stands for “table”, meaning a crontab is essentially a time table. These are special configuration files that contain a list of cron jobs that need to be executed. Each line within the crontab file represents a cron job and specifies when and how often a certain command/script is executed.
crontab is also the command to use when viewing and editing these configuration files. The two most useful commands are:
crontab -l: used to display the current crontab.
crontab -e: used to edit the current crontab using the editor defined in your environment variables.
What are the advantages of using Cron?
Automating processes can no doubt simplify our everyday lives. No one enjoys having to wake up in the middle of the night or interrupt their weekend fun because a backup or an update must be started, right? That is where cron fits in. Cron allows you to automatically run commands and scripts that have to operate during off-hours. Not only does Cron let you sleep in, it also has your back in case you forget to run a job manually… we’re only human after all.
The important thing to realize is that cron is not an interval scheduler, meaning that running a command every 7 minutes or every 2 days is not easily achievable using Cron (unlike intervals that fit well within a whole hour or day like every 10 minutes or every 2 hours) . The cron expression can't do just anything, but since it is used to running commands, with a few cool hacks, you can use it for just about any schedule you can dream of. Read more about the things Cron can’t do (and how to make it do them anyway) here.