Comparing cron and systemd for task automation
Timer Time
Cron and systemd are two options for automating tasks in Linux. Which is best? That all depends…
Automation is a cornerstone of modern Linux system administration, bringing efficiency in executing repetitive tasks. In the past, the venerable and reliable cron utility was the go-to tool for automating recurring tasks. In recent years, however, a more modern alternative has emerged as a competitor to the classic cron: systemd timers. Although the systemd init system [1] has been around since 2010, setting timers through systemd still seems exotic and new to many Linux users. A large contingent of the Linux crowd never really had a problem with cron and has never seen any reason to switch. Still, systemd offers advanced features that aren't available with cron, and once you get past the learning curve, systemd timers aren't as complicated as they look. We decided it was time to compare these two powerful automation techniques side by side. We'll start with brief descriptions, then compare some of the pertinent features, and we'll end with a look at a few practical examples. If you're in need of some additional background, see the cron tutorial later in this issue.
Cron for Task Automation
Cron, derived from the Greek word "chronos" meaning time, is a time-based job scheduler in Unix-like operating systems [2]. Cron allows users to schedule commands or scripts, known as cron jobs, to run periodically at fixed times, dates, or intervals. The behavior of cron is governed by crontab (cron table) files. A crontab file is a configuration file specifying the schedule and the command you wish to execute [3]. Users can create their own crontab files, and there is typically a system-wide crontab file for administrative tasks.
The fundamental unit of cron is the cron job, which is defined by a specific syntax within the crontab file. Each line in a crontab represents a job and consists of a cron expression followed by the command to execute. The standard cron expression comprises five fields representing the minute (0-59), hour (0-23), day of the month (1-31), month (1-12 or JAN-DEC), and day of the week (0-6 or SUN-SAT). These fields are separated by spaces, and each can contain specific values, ranges, lists, or the asterisk (*) to denote all possible values within that field. For instance, * * * * /path/to/command would execute the specified command every minute.
[...]
Buy this article as PDF
(incl. VAT)
