Back to Blog
Lesson 52 of the Linux: Linux Command Line for Developers course
LinuxSeptember 9, 20264 min read

Monitoring System Logs: A Practical Guide to journalctl

Master real-time log monitoring with journalctl. Learn to filter by service, tail output, and identify error patterns to troubleshoot your Linux servers efficiently.

linuxlogstroubleshootingjournalctlsystemddevops
Black and white photo of a Fabius anesthesia workstation in a hospital environment.

Previously in this course, we discussed System Performance Tuning to keep your server running at its peak. In this lesson, we shift from performance metrics to reactive troubleshooting: monitoring system logs. When your web server stops responding or a script fails, the logs are your primary source of truth.

Understanding the Systemd Journal

Most modern Linux distributions use systemd-journald to collect and store log data. Unlike traditional plain-text files in /var/log/, the journal is stored in a binary format. This allows for faster searching and metadata indexing, but it means you must use the journalctl command to read them.

Think of the journal as a central hub where the kernel, system services, and applications report their status. If you are familiar with Handling System Errors, you know that exit codes tell you that something failed; logs tell you why.

Real-Time Monitoring with journalctl -f

When you are actively debugging a broken web server or a hanging script, you need to see events as they happen. The -f (follow) flag is your best friend here. It functions just like tail -f for traditional logs, printing new entries to your terminal in real-time.

To monitor your web server (e.g., Nginx), run:

Bash
sudo journalctl -u nginx -f

This command restricts the output to only the nginx unit. If you see a timeout error, it will appear here the instant it occurs, allowing you to correlate it with the request you just sent.

Filtering Logs by Service and Time

One of the most powerful features of journalctl is the ability to filter by metadata. You rarely want to see the entire system log; you want to isolate the noise.

  • Filter by service: Use the -u flag as shown above.
  • Filter by time: You can use --since and --until.
    Bash
    # See logs from the last 10 minutes
    sudo journalctl --since "10 minutes ago"
    
    # See logs from a specific date
    sudo journalctl --since "2023-10-01 00:00:00" --until "2023-10-01 12:00:00"
  • Filter by priority: Logs have severity levels (0=emerg, 3=err, 4=warning, etc.). If you only want to see errors, use -p 3.

Identifying Error Patterns

When triaging an incident, do not just look at the last line. Look for patterns. If you see a repeating error, it usually indicates a systemic issue, such as a misconfigured path or a recurring connection timeout.

Using the power of pipes, you can refine your search:

Bash
# Search for 'failed' in the last hour for all services
sudo journalctl --since "1 hour ago" | grep -i "failed"

If you are dealing with a flood of logs, use less to scroll through them safely without overwhelming your terminal buffer:

Bash
sudo journalctl -u nginx --no-pager | less

Hands-on Exercise: Debugging the Web Server

For our ongoing project, let's simulate a log investigation.

  1. Open your terminal and ensure your web server service is running.
  2. Run journalctl -u nginx -f in one terminal window.
  3. In a second window, intentionally trigger a "404 Not Found" error by attempting to curl a non-existent file on your server (e.g., curl http://localhost/non-existent-file).
  4. Observe the log output in your first window. Can you identify the specific line that logs the 404 status code?

Common Pitfalls

  • Forgetting sudo: By default, you may not have permission to view all system logs. If you get an empty output or "permission denied," add sudo.
  • Ignoring the --no-pager flag: In scripts, journalctl will try to open a pager (like less). If you are piping to another command, your script might hang. Use --no-pager to output raw text.
  • Assuming binary logs are permanent: Depending on your configuration, the journal might be cleared on reboot. If you need logs for long-term auditing, consider shipping them to a dedicated log server, similar to the techniques discussed in Monitoring Deployed APIs.

FAQ

Q: How do I clear the journal to save disk space? A: Use sudo journalctl --vacuum-time=3d to remove logs older than 3 days.

Q: Why are my logs disappearing? A: Systemd logs can be configured to be volatile (stored in RAM). Check /etc/systemd/journald.conf and ensure Storage=persistent is set if you want logs to survive reboots.

Recap

We have moved from simple file inspection to active system observation. By leveraging journalctl -f and time-based filtering, you can now isolate service-specific errors and troubleshoot failures in real-time. Consistent log monitoring is the foundation of reliable infrastructure, preventing minor warnings from becoming major outages.

Up next: We will dive into Advanced Permissions (ACLs) to manage complex access control requirements for our web server.

Similar Posts