Overview of Debian Timers and System Logs
Debian systems offer two primary timer mechanisms for scheduling recurring tasks: Cron (the traditional text-based solution) and systemd timers (the modern, integrated alternative). Both rely on the system’s logging infrastructure to record execution details, enabling administrators to monitor task performance, troubleshoot failures, and maintain audit trails. The logs are centralized in journald (systemd’s logging daemon), which stores structured, searchable logs with metadata like timestamps, unit names, and priorities.
Cron and System Logs
Cron, a daemon that runs user-defined scripts or commands at specified intervals, automatically logs execution details to the system log. By default, cron entries in /var/log/syslog
(or /var/log/cron.log
on some systems) include:
For example, a cron job running */5 * * * * /path/to/script.sh
will generate entries like:
May 10 14:30:01 debian CRON[12345]: (root) CMD (/path/to/script.sh)
This allows administrators to track when tasks ran and identify issues (e.g., a script failing with a non-zero exit code).
Systemd Timers and System Logs
Systemd timers (defined in .timer
files) and their associated services (.service
files) integrate tightly with journald, providing more granular and structured logging. Here’s how the relationship works:
.service
file defines the task to execute (e.g., /path/to/script.sh
). By default, the service’s output (stdout/stderr) is sent to journald. You can explicitly configure this in the [Service]
section:[Service]
StandardOutput=journal
StandardError=journal
SyslogIdentifier=mytask # Tags logs with "mytask" for easy filtering
.timer
file (e.g., mytask.timer
) schedules the service. When the timer triggers, journald records the activation event (e.g., mytask.timer: Triggering mytask.service
).journalctl
to query logs for a specific timer or service:
journalctl -u mytask.service
journalctl -u mytask.timer
journalctl -u mytask.service --since "yesterday" -p err
.Key Advantages of systemd Timer Logging
Compared to Cron, systemd timer logging offers several benefits:
/var/log/syslog
).journalctl -u mytask.service --since "2025-10-01"
).journalctl -f
to follow logs as they are generated (similar to tail -f
).After=network.target
), and these dependencies are logged, helping troubleshoot failed triggers.Common Log Management Tasks
journalctl
journalctl -u mytask.service
journalctl -u mytask.timer
journalctl -p err
journalctl --since "yesterday" --until "now"
journalctl -u mytask.service
/etc/systemd/journald.conf
(set Storage=persistent
) and restarting journald (systemctl restart systemd-journald
).Example: Logging a Systemd Timer Task
To create a timer that logs a message every minute:
/etc/systemd/system/log-task.service
:[Unit]
Description=Log Rotation Task
[Service]
Type=oneshot
ExecStart=/usr/bin/logger -t "LogTask" "Running log rotation at $(date)"
SyslogIdentifier=logtask
/etc/systemd/system/log-task.timer
:[Unit]
Description=Run Log Task Every Minute
[Timer]
OnCalendar=*-*-* *:*:00 # Every minute
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl enable --now log-task.timer
journalctl -u log-task.service
Output will show entries like:Oct 10 14:30:00 debian logger[12345]: Running log rotation at Fri Oct 10 14:30:00 UTC 2025
.