Sub topic 6
Capturing Output and Logs
Assume you can edit shell scripts, read exit codes, and use basic Linux commands such as grep, less, and journalctl. This chapter shows how to preserve a recurring job’s output and make failures visible.
Why output matters
A scheduled job usually has two output streams:
- Standard output, called
stdout, contains normal messages. - Standard error, called
stderr, contains warnings and errors.
A job can run at the correct time and still fail. If nobody records its output or checks its exit status, the failure may remain hidden.
For example, this command records both streams in one file:
/usr/local/bin/sync.sh >> /var/log/sync.log 2>&1The >> operator appends stdout to the log. The 2>&1 part sends file descriptor 2, stderr, to the same destination as stdout. Keep the order unchanged. > logfile 2>&1 sends both streams to the file, while > logfile 2> other.log sends them to separate files.
Use >> when you want to preserve earlier runs. Use > only when replacing the file is intentional.
Capturing output from cron
Cron can send job output by email when a mail system is configured. The MAILTO variable controls the recipient:
[email protected]
PATH=/usr/local/bin:/usr/bin:/bin
*/15 * * * * /usr/bin/flock -n /run/lock/sync.lock /usr/local/bin/sync.sh >> /var/log/sync.log 2>&1This configuration sets PATH explicitly because cron provides a smaller environment than an interactive shell. It also appends output to /var/log/sync.log.
The cron user must have permission to create and write the log. A root-owned job can write under /var/log, but a job running as an ordinary user may need a directory owned by that user, such as /var/log/my-job/, or logging through another service.
Add timestamps to script messages so you can connect an event to a run:
printf '%s starting sync\n' "$(date --iso-8601=seconds)"Do not write passwords, access tokens, or private data to logs. Log identifiers and useful summaries instead.
Reading systemd logs
A systemd timer normally starts a service. The service’s standard output and error output go to the systemd journal unless you configure another destination.
[Service]
Type=oneshot
User=backup
ExecStart=/usr/local/bin/backup.sh
StandardOutput=journal
StandardError=journalInspect recent output with:
sudo journalctl -u backup.service --since today
sudo journalctl -u backup.service -n 50 --no-pager
sudo journalctl -u backup.service -fThe -f option follows new messages as they arrive. Filter by priority when you need errors:
sudo journalctl -u backup.service -p warning --since "1 hour ago"The journal records messages, but retention depends on system configuration. If logs must survive reboots or remain available for a specific period, configure persistent journal storage or forward messages to a central logging system.
Choose a retention strategy
| Scheduler | Default destination | Useful inspection command |
|---|---|---|
| cron | Mail, or a file after redirection | `less /var/log/sync.log` |
| systemd | systemd journal | `journalctl -u backup.service` |
File logs need rotation. Log rotation renames old files, compresses them, and removes files older than a chosen age. Without rotation, a frequently running job can fill the disk. Journal retention also needs limits, but systemd can enforce them centrally.
Exercise
In a cron entry, what does 2>&1 do, and why should it appear after the output redirection?
Show answer
It sends standard error to the same destination as standard output. It must follow the output redirection so descriptor 2 receives the destination already assigned to descriptor 1.