What this is about
A monitoring pipeline does not run once, it runs continuously: fetch feeds, classify results, send a heartbeat. Something has to control these steps in time. On Linux the main choices are cron and systemd timers. Both can start the same job, but they differ clearly in error handling, logging and maintainability.
Cron: small and familiar
Cron reads a table with five time fields and a command. One line is enough to start a collector every 15 minutes.
*/15 * * * * /usr/bin/python3 /opt/pipeline/collector.py >> /var/log/pipeline/collector.log 2>&1
The strengths lie in its simplicity. The syntax is known everywhere, every distribution ships cron, and for a single job there is hardly a shorter solution. The weaknesses show up in operation:
- The environment is minimal. Variables such as PATH differ from your interactive shell, which causes errors that only occur under cron.
- Output ends up only where you redirect it. Without a redirect it is lost or sent as local mail.
- If the machine is off at the scheduled time, the run is skipped and nobody catches up.
- There are no dependencies. Cron does not know whether the network or another service is ready.
systemd timers: more control
A systemd timer consists of two files: a service unit that describes what runs and a timer unit that describes when.
# collector.service
[Unit]
Description=Feed-Collector
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/bin/python3 /opt/pipeline/collector.py
EnvironmentFile=/etc/pipeline/env
# collector.timer
[Unit]
Description=Collector alle 15 Minuten
[Timer]
OnCalendar=*:0/15
Persistent=true
RandomizedDelaySec=30
[Install]
WantedBy=timers.target
After systemctl enable --now collector.timer the schedule is active. Three options make the difference in daily use:
- Persistent=true catches up a missed run as soon as the machine is on again.
- RandomizedDelaySec spreads start times slightly so that several jobs do not hit the system at exactly the same moment.
- After= and Wants= make sure the job starts only after the network is up.
Logging and troubleshooting
The output of a service job lands in the journal automatically. With journalctl -u collector.service you see all runs including exit codes, and with systemctl list-timers the next start. With cron you have to maintain and rotate your own log files.
For failure notifications systemd offers OnFailure=: if a job fails, a second unit starts that sends a push message, for example. With cron you build this into the script yourself, usually with a wrapper that checks the exit code.
A special case: overlapping runs
If a run takes longer than the interval, cron starts several instances in parallel unless you protect the job with a lock, for example with flock. With systemd a timer does not start a second run while the service is still active. That removes a typical source of errors.
When it is worth it (and when not)
Cron is enough when you have a few uncritical jobs, the environment is stable and a missed run does not matter. For a single heartbeat, one crontab line is perfectly fine.
systemd timers pay off as soon as several components work together: collector, classification, dispatcher, heartbeat. Then the journal, catching up on missed runs, dependencies and clean failure notification count. The extra effort is two files per job, which you create once as a template and copy.
A mixed setup is possible too, as long as it is clearly documented which job is defined where. The only rule is never to schedule the same job in both systems at once.