Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Follow a log in real time on Linux

By updated

Improve this page

When a service crashes or behaves oddly, the first reflex is to open its logs. But if you open a static file, you see a snapshot. What you need is a live stream: see the lines as they arrive, filter the errors while a deployment runs, watch several files at the same time.

On Linux, two tools cover this need: tail -f for plain files, journalctl -f for systemd journals. This guide shows you how to use them alone and together.

What you will learn

  • Follow a log file in real time with tail -f and --follow=name
  • Filter errors in real time in a pipeline with grep --line-buffered
  • Watch several files at once with tail -f
  • Query journalctl by unit, priority, time range or boot
  • Choose the right tool for the log's source

In which context?

You are deploying a new version of an application and you want to see the logs when the service restarts. A cron job runs at night and you suspect an error you cannot reproduce during the day. A service is intermittent and you want to be alerted as soon as ERROR appears, without running a command every 30 seconds. Real-time log watching is a first-line skill in system administration: it gives you the information at the right moment, during the event, not after.

tail, follow plain files

Many services still write to text files under /var/log/, independently of systemd: nginx, apache, postgresql, or any home-grown application that redirects its output. For those, tail is the reading tool, and its follow mode turns a one-off read into continuous observation. Two points deserve your attention: file rotation and the behaviour of the buffer when you plug a filter behind it.

The basics

tail shows the last lines of a file. Without options, it shows 10:

Terminal window
tail /var/log/syslog # last 10 lines
tail -n 50 /var/log/syslog # last 50 lines
tail -n +100 /var/log/syslog # from line 100

Follow mode: tail -f

-f (follow) opens the file and shows the new lines as they are written. It does not give the prompt back: it stays active until Ctrl+C:

Terminal window
tail -f /var/log/nginx/access.log

tail -f vs tail --follow=name

This is an important distinction in production. A file descriptor is the number the kernel assigns to an open file: it stays attached to the content even if the file is renamed. tail -f holds on to this descriptor, while --follow=name checks the path again at regular intervals and reopens the new file as soon as it appears. The --retry option completes the second form by making it patient if the file disappears for a few moments.

OptionBehaviour
tail -fFollows the file descriptor: after a rotation, it keeps reading the old renamed file
tail --follow=name --retry (or tail -F)Follows the file name, survives logrotate rotation
Terminal window
# For logs with rotation (nginx, apache, syslog...)
tail --follow=name --retry /var/log/nginx/access.log

Follow several files

tail -f accepts several arguments. It prefixes each block with the file name:

Terminal window
tail -f /var/log/nginx/access.log /var/log/nginx/error.log
==> /var/log/nginx/access.log <==
192.168.1.10 - - [09/Apr/2026:10:05:00] "GET /api/users HTTP/1.1" 200 523
==> /var/log/nginx/error.log <==
2026/04/09 10:05:01 [error] 1234#0: upstream timed out

Filter in real time

On a busy server, a raw tail -f scrolls too fast to be read. The reflex is to plug a filter behind it, with a pipe (|), to keep only the lines you care about. This combination has a trap specific to real time: by default, commands in the middle of a pipe only write their output in blocks, which freezes the display for minutes.

grep --line-buffered: the essential option

In a normal pipeline, grep accumulates its output in a buffer before writing it, which breaks the real-time effect. --line-buffered forces writing line by line:

Terminal window
# Show only errors and warnings
tail -f /var/log/app.log | grep --line-buffered -iE 'error|warn'
# Exclude the monitoring requests (/health, /metrics)
tail -f /var/log/nginx/access.log | grep --line-buffered -v '/health\|/metrics'
# Show HTTP 5xx errors in colour
tail -f /var/log/nginx/access.log | grep --line-buffered --color=always '" 5[0-9][0-9] '

Chain several filters

Nothing prevents chaining several grep: the first selects, the following ones exclude the remaining noise. Each link of the pipe has its own buffer, so --line-buffered must be repeated on each of them, except the last one, which writes directly to the terminal. Forgetting the option on a single link is enough to freeze the whole display.

Terminal window
# Errors but not the monitoring errors
tail -f /var/log/app.log \
| grep --line-buffered 'ERROR' \
| grep --line-buffered -v 'healthcheck'

less +F, the interactive alternative

less +F starts in follow mode and lets you browse the history during the session:

Terminal window
less +F /var/log/syslog
  • Ctrl+C: stops following, lets you scroll with the arrows
  • F: resumes real-time following
  • q: quits

It is useful when you want both to see the new lines AND to go back through the history without quitting.

journalctl, the systemd journals

journalctl queries systemd's binary journal. Unlike plain files, this journal centralises the messages of every service, with metadata, priorities and boot numbering.

Real-time following

-f shows the latest entries then stays active, adding each new line as services write it, until Ctrl+C. Filters add up with this mode: -u restricts to one or several systemd units, -p to a priority level. Repeating -u shows the units interleaved in chronological order, which shows the real sequence between two services.

Terminal window
journalctl -f # every unit, live
journalctl -f -u nginx # nginx service only
journalctl -f -u nginx -u postgresql # nginx AND postgresql
journalctl -f -p err # errors only, live

Filter by systemd unit

The name passed to -u is that of the systemd unit, as systemctl list-units shows it; the .service suffix is optional. --no-pager turns off less and writes directly to the terminal, which is essential in a script or when you want to chain the output into another tool. -n limits the output to the most recent lines, the reflex not to scroll through months of history.

Terminal window
journalctl -u nginx --no-pager # nginx's whole history
journalctl -u nginx -n 50 --no-pager # last 50 lines
journalctl -u nginx -f # live

Filter by priority

The levels follow the syslog convention (from the most to the least critical):

LevelValueMeaning
emerg0System unusable
alert1Immediate action required
crit2Critical condition
err3Errors
warning4Warnings
notice5Notable normal conditions
info6Informational messages
debug7Debug messages

-p err filters the levels err and more critical (0 to 3):

Terminal window
journalctl -p err --since "1 hour ago" --no-pager
journalctl -p warning --since today --no-pager # warnings and more critical

Filter by time range

--since and --until accept an absolute date (YYYY-MM-DD HH:MM:SS) as well as a relative expression in English, computed from the machine's current time. That is what lets you go back over an incident that is already over: you frame the window reported by the monitoring and you read only those minutes. Beware of the time zone: journalctl shows the server's local time, not yours.

Terminal window
journalctl --since "2026-04-09 10:00:00" --until "2026-04-09 11:00:00" --no-pager
journalctl --since today --no-pager
journalctl --since yesterday --no-pager
journalctl --since "1 hour ago" --no-pager

Filter by system boot

journald assigns an identifier to each boot of the system and lets you refer to it by a relative number: 0 for the current boot, -1 for the previous one. This split is precious after an unwanted reboot, because the last lines before the outage are in the previous boot, not the current one. If --list-boots returns only one line, the journal is not persistent and the history stops at the last boot.

Terminal window
journalctl -b --no-pager # current boot
journalctl -b -1 --no-pager # previous boot
journalctl --list-boots # list every known boot

It is useful after a crash: journalctl -b -1 -p err shows the errors of the previous boot.

Kernel messages

-k keeps only the entries emitted by the kernel: hardware detection, disk errors, processes killed for lack of memory, packets rejected by the firewall. It is the first thing to look at when the failure seems to come from the machine rather than the application. Unlike dmesg, journalctl timestamps these messages with the real date, which lets you cross-check them with the services' logs.

Terminal window
journalctl -k --no-pager # kernel messages (dmesg equivalent)
journalctl -k --since "1 hour ago" --no-pager

Combine journalctl and grep

journalctl filters on metadata (unit, priority, date), not on the message content. As soon as the search bears on a precise text, a pipe to grep or awk takes over. --no-pager then becomes mandatory: without it less intercepts the output and the pipe receives nothing usable. The second example counts the occurrences per service, a quick way to spot the noisiest component. The fifth field is process[PID]:: the sub() removes the PID, otherwise each process would be counted separately.

Terminal window
# Look for failed SSH connections (ssh unit on Debian and Ubuntu)
journalctl -u sshd --since today --no-pager | grep 'Failed\|Invalid'
# Count the errors per service over the last hour
journalctl -p err --since "1 hour ago" --no-pager \
| awk '{sub(/\[[0-9]+\]:$|:$/, "", $5); print $5}' | sort | uniq -c | sort -rn

Choose between tail -f and journalctl

The choice criterion is not a preference, it is the log's source. If the service itself writes to a file under /var/log/, tail reaches it directly; if it only writes to its standard output, systemd captures the stream and only journalctl sees it. When in doubt, journalctl -u <service> answers at once: if it only shows the service's starts and stops, its application messages go elsewhere, therefore to a file.

SituationRecommended tool
Service with logs in /var/log/*.logtail -f
systemd service without a dedicated log filejournalctl -f -u <service>
Filter by priority or time rangejournalctl
Post-crash investigation (previous boot)journalctl -b -1
Follow several non-systemd filestail -f file1 file2
Interactive mode with history navigationless +F

Administration use cases

The commands below combine the building blocks seen above on real operational situations: accompanying a deployment, reacting to an error line, understanding why a service refuses to start, ranking the noisiest components. The last example skips no line: journalctl writes no header, and an NR>1 would throw away a real entry (measured on systemd 255). The while read loop deserves a remark: it handles each line as soon as it arrives, and only exists thanks to the --line-buffered on the grep before it. The commented logger line would send the alert to syslog instead of printing it.

Terminal window
# Watch a deployment: app + nginx logs at the same time
tail -f /var/log/app/production.log /var/log/nginx/error.log
# Alert on errors during a deployment
tail --follow=name --retry /var/log/app/production.log \
| grep --line-buffered 'ERROR\|FATAL' \
| while read -r line; do
echo "[ALERT] $line"
# logger -t deploy_monitor "$line" # optional: send to syslog
done
# Diagnose why a service did not start
journalctl -u my-service -b --no-pager | tail -n 30
# See every error since yesterday
journalctl -p err --since yesterday --no-pager | grep -v 'kernel'
# Top of the noisiest services in errors (last hour)
journalctl -p err --since "1 hour ago" --no-pager \
| awk '{sub(/\[[0-9]+\]:$|:$/, "", $5); print $5}' | sort | uniq -c | sort -rn | head -10

Key points

  • tail -f follows a file by descriptor; --follow=name --retry (or tail -F) survives rotation: use the second in production.
  • grep --line-buffered is mandatory in a pipeline with tail -f to avoid silent buffering.
  • journalctl -f -u <service> is the systemd equivalent of tail -f, with no rotation risk.
  • journalctl -b -1 -p err is the first reflex after a crash: errors of the previous boot.
  • less +F lets you browse the history without leaving follow mode.

Knowledge check

Check that the essentials of this guide stuck. The questions cover only what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

6 questions
6 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer