Skip to content

Linux Foundations / Manage a Linux host ยท Step 46 of 50

Lesson: Managing Log FilesSwitch
โ† Linux Foundations

System administration

Managing Log Files

Keep logs from filling the disk with logrotate and journal size limits.

Logs grow without end unless something trims them. A full /var can stop services, so Linux rotates logs: the current file is renamed, a fresh one is started, and old copies are compressed and eventually deleted.

logrotate

logrotate is the usual tool for text logs. It is run periodically, by a systemd timer or a daily cron job depending on the distribution.

Global settings are in /etc/logrotate.conf. Packages and administrators add files to /etc/logrotate.d/.

A configuration for an application:

/var/log/myapp/*.log {
    weekly
    rotate 4
    compress
    delaycompress
    missingok
    notifempty
    create 0640 myapp adm
}
  • weekly: rotate once a week
  • rotate 4: keep four old copies
  • compress: gzip old logs
  • delaycompress: leave the most recent old copy uncompressed until next time
  • missingok: no error if the log is absent
  • notifempty: skip empty files
  • create: make a new file with this mode, owner and group

Programs that keep files open

A program may keep writing to the renamed file. A postrotate script, closed by endscript, can tell it to reopen its log. Another choice is copytruncate, which copies the file and then empties the original. It needs no signal, but lines written between the two steps can be lost.

Testing

Debug mode shows what would happen without changing anything:

$ sudo logrotate -d /etc/logrotate.d/myapp

To force a rotation now, use -f:

$ sudo logrotate -f /etc/logrotate.d/myapp

Testing one file this way skips the global defaults from /etc/logrotate.conf.

Journal size

logrotate does not manage the journal. journald has its own limits:

$ journalctl --disk-usage
Archived and active journals take up 120.0M in the file system.
$ sudo journalctl --vacuum-time=2weeks
$ sudo journalctl --vacuum-size=200M

Vacuuming removes archived journal files, not the active ones. For a permanent cap, set SystemMaxUse=200M under [Journal] in /etc/systemd/journald.conf (or a drop-in file), then restart systemd-journald.

Do not just delete

If you remove a log that a program still has open, the space is not freed until the program closes it. To empty one in place, use sudo truncate -s 0 file. Check you do not need its contents for an audit or investigation first.

Try it

Look through /etc/logrotate.d/ and read one entry. Run logrotate -d on it with sudo, then check journalctl --disk-usage.

Check your understanding

Managing Log Files

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.