In this lesson, you will learn how to use systemd template units to generate multiple, independent timer instances from a single configuration file.
The Challenge of Redundancy
When you need to run similar tasks—such as backups for different directories or database cleanups for various instances—you might be tempted to copy-paste identical timer and service files. This approach is prone to configuration drift and makes maintenance difficult. Systemd offers a cleaner solution: Template Units. By using the @ symbol, you can create a single "blueprint" that accepts variables to define specific instances.
How Templates Work
A template unit file ends with @ before the extension (e.g., backup@.service). When you enable or start a unit, you provide an argument after the @, which systemd makes available inside the unit as the %i specifier.
For example, consider a backup service:
# /etc/systemd/system/backup@.service
[Service]
ExecStart=/usr/local/bin/backup-script.sh %i
To create a timer for this, you create a corresponding timer template:
# /etc/systemd/system/backup@.timer
[Unit]
Description=Timer for %i backup
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
Deploying Instances
Once these files exist, you don't enable backup@.timer. Instead, you enable specific instances by passing the directory or identifier as the argument:
systemctl enable --now backup@home.timer
systemctl enable --now backup@var-www.timer
Each instance acts as an independent process. If you want to check the status of the "home" instance, you simply run systemctl status backup@home.timer. This pattern allows you to scale your automation horizontally without cluttering your filesystem with redundant unit files. By using the %i specifier, the underlying script knows exactly which target it is processing, keeping your logic DRY (Don't Repeat Yourself) and your system configuration manageable.
Key Takeaway: Use the @ template syntax to transform a single generic timer definition into multiple dynamic instances, using the %i specifier to pass unique configuration parameters to each job.