You can schedule system commands with Cron on Linux. What is Cron? It’s a background service that, when configured, can execute any command, script or program on a schedule. Best of all, it’s completely customizable.
Cron is very useful, but the command syntax is incredibly confusing, especially for the average user. For this reason, we’re going to cover how to understand Cron easily, and it’s command syntax so that you can schedule system commands on your Linux machine automatically!
For the most part, Cron is pre-installed and set up on a lot of Linux distributions. The reason for this is that Cron is central to a lot of command-line automation in the Linux SysAdmin industry.
Even if Cron is set up on most distributions, that doesn’t mean that every single Linux operating system in existence has it. So, before we talk about how to use Cron, it’s imperative you install it on your OS.
In this section, we’ll focus on installing Cronie, a tool found by default on many Redhat Linux distributions. The reason? It has standard functionality for Cron, and the packages are on the software repositories of most Linux distributions.
To determine if you need to install the Cronie package, launch a terminal window and enter the command below. If nothing happens, your system doesn’t have a way to schedule system commands, and you should install Cronie.
Ubuntu and Debian do not support Cronie. However, these Linux distributions often come configured with Cron out of the box. If for some reason your system can’t use Crontab, consult with the Debian or Ubuntu wiki pages for help.
Arch Linux is a do-it-yourself Linux distribution and users are expected to install every single package on their system and configure it themselves. As a result, many users neglect to install an app that lets them schedule system commands with Cron. Luckily, the Cronie application is in the “Arch Linux Core” software repository. To get Cronie, enter the Pacman command below.
sudo pacman -S cronie
Fedora Linux is a workstation operating system, so chances are pretty good that you already can interact with Cron and the Crontab. Still, if for some reason you can’t, Cronie is easily installed with the DNF packaging tool.
sudo dnf install cronie -y
OpenSUSE Leap (the release of SUSE that most users are using) is a workstation OS, much like Fedora, so, Cron should already be set up. However, if it isn’t, install Cronie with the following Zypper command.
sudo zypper install cronie
On an obscure Linux distribution that doesn’t have a Cron system? Lucky for you, Cronie is open source, and the build instructions are very straightforward. To learn more, go to this page.
Schedule system commands with Cron
Scheduling system commands with Cron requires the “Crontab”. The “Crontab” is a file that keeps your operations and passes them off to the Cron daemon to run.
To access the Crontab on your system, launch a terminal and run the following command below. Be sure to include the EDITOR=nano line, or you’ll be given a much more challenging editor, in place of something easy like Nano.
EDITOR=nano crontab -e
After running the command, the text editor will open up to a new blank file. This blank file is where your commands go.
In Cron, scheduling is relatively complicated. It goes by minutes, hours, days, months and weekdays. However, we won’t be going over how to write out a Cron schedule manually. Instead, we’ll point you to the Crontabgenerator website, as it makes things much more comfortable.
Once you’ve made it to the generator, use the checkboxes to pick a schedule. Then, when you’re satisfied with the schedule, move down to the “Command To Execute” box.
In the “Command To Execute” box, write out the terminal commands you want your Linux computer or server to execute automatically. Keep in mind that “commands” aren’t limited to Linux commands like “apt update”, “ping”, etc. They can be anything! As long as your command works in the terminal, it’ll work in Cron!
Finished filling out your command in the “Command To Execute” box? Click the green “Generate Crontab Line” button to output a text line. Then, highlight the text on the output page and return to the Crontab text editor.
Press Ctrl + Shift + V to paste the code into Nano. Then, save it with Ctrl + O. Once your Crontab file is saved, the hard work is done. Exit the editor with Ctrl + X.
Note: those not using Cronie may need to execute crontab -e after editing to ensure the new file goes into effect.
When the edit is done, the Cron background service will take notice and run it on the schedule you specified!