การบันทึกเหตุการณ์แบบรวมศูนย์และการหมุนเวียนด้วย journald และ logrotate
ดูแลระบบอัตโนมัติให้ทำงานได้ดีด้วยการอ่านบันทึก systemd ผ่าน journalctl และป้องกันไม่ให้ไฟล์บันทึกเต็มดิสก์ด้วย logrotate
การบันทึกเหตุการณ์แบบรวมศูนย์และการหมุนเวียนด้วย journald และ logrotate เป็นบทเรียน Linux Command Line Mastery ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Linux Command Line Mastery ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การบันทึกเหตุการณ์แบบรวมศูนย์และการหมุนเวียนด้วย journald และ logrotate”
ดูแลระบบอัตโนมัติให้ทำงานได้ดีด้วยการอ่านบันทึก systemd ผ่าน journalctl และป้องกันไม่ให้ไฟล์บันทึกเต็มดิสก์ด้วย logrotate คุณปฏิบัติ Linux Command Line Mastery ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Linux Command Line Mastery หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Linux Command Line Mastery บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การบันทึกเหตุการณ์แบบรวมศูนย์และการหมุนเวียนด้วย journald และ logrotate” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Linux Command Line Mastery นี้ได้ไหม
ได้ บทเรียน Linux Command Line Mastery ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตั้งเวลางาน: `cron` และ `at`
- การจัดการบริการ: `systemctl` (systemd)
- การทำงานอัตโนมัติด้วยฟังก์ชันเชลล์
- การบันทึกเหตุการณ์แบบรวมศูนย์และการหมุนเวียนด้วย journald และ logrotate