使用 journald 与 logrotate 集中记录日志并轮换
通过 journalctl 读取 systemd 日志,并使用 logrotate 防止日志文件占满磁盘,从而保持自动化系统健康。
使用 journald 与 logrotate 集中记录日志并轮换 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Automation Needs Logs
Cron jobs, systemd services, and shell functions all produce output. To run unattended systems you must capture, search, and rotate that output. Enter journalctl and logrotate.
What Is journald?
systemd-journald collects logs from services you manage with systemctl into a structured binary journal you query with journalctl.
Viewing Logs
Plain journalctl shows everything; -e jumps to the end (newest entries).
journalctl -eFiltering by Service
-u limits the journal to one unit, perfect for debugging a single service.
journalctl -u nginx.serviceFollowing Logs Live
-f streams new entries as they arrive, like tail -f for systemd.
journalctl -u nginx.service -fFiltering by Time and Priority
--since and -p narrow results. Here we show errors from the last hour.
journalctl --since '1 hour ago' -p errControlling Journal Size
Journals can grow large. --vacuum-size trims them to a target size.
sudo journalctl --vacuum-size=200MPlain-Text Logs and logrotate
Many apps still write to /var/log/*.log. logrotate rotates, compresses, and deletes these on a schedule so disks do not fill.
cat /etc/logrotate.confA logrotate Rule
Drop a config in /etc/logrotate.d/. This rotates daily, keeps 7 copies, and compresses old ones.
/var/log/myapp/*.log {
daily
rotate 7
compress
missingok
notifempty
}Testing logrotate
-d does a debug dry run that shows what would happen without changing files; -f forces an immediate rotation.
sudo logrotate -d /etc/logrotate.d/myappTying It to Automation
Have cron jobs and functions log to a dedicated file, manage that file with a logrotate rule, and use journalctl for everything run as a systemd service. Now nothing fills the disk silently.
Quick Check
Which journalctl option shows logs for a single systemd service?
Recap
You can keep automated systems observable and tidy:
journalctl -u,-f,--since,-pquery systemd logs--vacuum-sizetrims the journallogrotaterotates/compresses plain-text logs via/etc/logrotate.d/logrotate -ddry-runs your config
常见问题解答
「使用 journald 与 logrotate 集中记录日志并轮换」课时是免费的吗?
是的 — 「使用 journald 与 logrotate 集中记录日志并轮换」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。
「使用 journald 与 logrotate 集中记录日志并轮换」这节课中我会学到什么?
通过 journalctl 读取 systemd 日志,并使用 logrotate 防止日志文件占满磁盘,从而保持自动化系统健康。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Command Line Mastery 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 journald 与 logrotate 集中记录日志并轮换」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?
能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 任务调度:`cron` 和 `at`
- 服务管理:`systemctl`(systemd)
- 使用 Shell 函数自动化任务
- 使用 journald 与 logrotate 集中记录日志并轮换