Course contentsShow
← Linux
Lesson 1295 of 1,81911. Automation and System SchedulingFree lesson

Combining Path and Timer Triggers

Build hybrid automation that reacts to both file events and time-based thresholds.

Combining Path and Timer Triggers

In this lesson, you will learn how to create resilient automation by layering file-system monitoring with time-based scheduling to ensure tasks run exactly when needed—even if a specific event is missed.

The Hybrid Automation Pattern

Sometimes, waiting for a file to change isn't enough. Perhaps you have a data-processing script that should run whenever a new file arrives in a directory (via a .path unit), but you also have a safety requirement that the script must run at least once every hour to ensure system consistency, even if no new files have appeared (via a .timer unit).

In systemd, you don't need to choose one or the other. You can trigger the same .service unit from both a .path and a .timer unit simultaneously.

Implementing the Dual-Trigger Setup

To combine these, you create three files: the service itself, the path monitor, and the timer. All three should share the same base name (e.g., backup.service, backup.path, and backup.timer).

For example, your backup.path watches a directory:

[Path]
PathChanged=/mnt/data/incoming/
Unit=backup.service

And your backup.timer acts as the heartbeat:

[Timer]
OnCalendar=hourly
Unit=backup.service

Why Combine Them?

By decoupling the execution logic from the triggers, the backup.service remains blissfully unaware of what caused it to start. This is the ultimate "decoupling" strategy:

  1. Immediate Reaction: The .path unit triggers the service the moment a file is dropped, providing low latency.
  2. Fail-safe Guarantee: The .timer unit acts as a watchdog. If the kernel's inotify queue overflows or a file event is somehow missed, the timer ensures the task completes periodically regardless.

Because you have already learned about Task Idempotency, you know that running the service twice (once by path, once by timer) will not cause errors. If the path trigger finishes the job, the subsequent timer execution will simply see that there is no work to do and exit cleanly.

Key Takeaway: Combining path and timer triggers provides both immediate reactivity and guaranteed periodic execution, ensuring your automation is both responsive and robust.