Chain: programs to journald (healthy), then rsyslog active but reading nothing, messages and secure frozen at 09:02, CrowdSec reading no lines and monitoring alerting at 10:17; at the bottom, restarting rsyslog writes 683 lines back
|

Restarting journald froze rsyslog: seventy-five minutes of silent log files and a blind CrowdSec

Some failures make noise and some make none. This one was of the second kind: for seventy-five minutes, three of my servers stopped writing to /var/log/messages and /var/log/secure, with no failed service, no error line, and it was a change I had just made myself, for good reasons, that caused it. The only witness was…

Some failures make noise and some make none. This one was of the second kind: for seventy-five minutes, three of my servers stopped writing to /var/log/messages and /var/log/secure, with no failed service, no error line, and it was a change I had just made myself, for good reasons, that caused it. The only witness was my monitoring, which noticed that another tool had stopped reading anything.

The context: a journal that didn’t survive crashes

It starts with a machine that dies abruptly. One of the machines in my small Kubernetes cluster crashes overnight, and after the reboot I want to know what happened just before. The natural command is journalctl -b -1, which shows the previous boot’s journal. It answers that no persistent journal exists. On that machine, and on two others, systemd-journald kept everything in memory, in /run/log/journal, and a reboot erased it all.

That isn’t an exotic setting. journald has a Storage= parameter with four values. volatile keeps the journal in memory only. persistent writes it to disk in /var/log/journal, creating the directory if needed. auto, the default, writes to disk only if /var/log/journal already exists, and that’s where the trap hides: on an install where nobody created that directory, the default amounts to volatile. Finally none discards everything. My older servers had the directory, the newer ones didn’t, and nobody noticed, since journalctl behaves the same in both cases, until the first crash.

The fix looked trivial. My common Ansible role already dropped a configuration file to cap the journal’s size; I added Storage=persistent, except on two Raspberry Pis running from SD cards, where constant writes would wear the card out. SystemMaxUse=200M bounds the space used, and journald deletes the oldest files when it reaches the limit.

# /etc/systemd/journald.conf.d/00-retention.conf
[Journal]
Storage=persistent
SystemMaxUse=200M

The role restarts journald when that file changes, then runs journalctl --flush, which moves what’s still in memory to disk without waiting for the next boot. I applied it, checked that /var/log/journal existed and was filling up on all three machines, and moved on.

The symptom: an alert about another program

An hour later, my monitoring raises an unexpected alert: CrowdSec, on one of those machines, hasn’t read a single line from its log files for an hour. CrowdSec is the tool that watches for intrusion attempts and bans aggressive addresses; among other things it reads /var/log/secure, where SSH logins land. If it reads nothing, it sees nothing, and an attack would go unnoticed.

Luckily I had a rule for exactly this case, because a blind CrowdSec never complains: it runs, it’s active, it waits for lines that never come. The first reflex is to check whether the files themselves still move.

$ date -r /var/log/messages +%H:%M; date -r /var/log/secure +%H:%M; date +%H:%M
09:02
09:02
10:17
$ journalctl -u rsyslog --since 08:50 --no-pager
rsyslogd[1319]: imjournal: journal files changed, reloading...  [v8.2604.0]

Both files hadn’t changed since 09:02, while it was 10:17. And the journal of rsyslog, the daemon that writes those files, held a single relevant line at 09:02: journal files changed, reloading. After that, nothing. No error, no stop, and systemctl status rsyslog proudly showed active (running). The two other machines I had changed were in exactly the same state; only this one fired the alert, simply because the others had less traffic to watch.

Chain: programs to journald (healthy), then rsyslog active but reading nothing, messages and secure frozen at 09:02, CrowdSec reading no lines and monitoring alerting at 10:17; at the bottom, restarting rsyslog writes 683 lines back
Everything is green except what matters: the text files stopped moving, and CrowdSec with them.

Why rsyslog depends on journald

On a modern Rocky Linux, rsyslog no longer receives messages directly from programs. They all go to journald, and rsyslog reads them back from the journal through its imjournal input module. That module remembers its position in a state file, a cursor pointing at the last entry read, so it resumes at the right place after rsyslog restarts. The classic text files are therefore only a copy of the journal, made by rsyslog.

When I restarted journald with persistent storage, the journal files moved: from /run/log/journal to /var/log/journal. imjournal noticed and said so in its own log, then stopped reading. I won’t claim to know the internal detail of that disconnect; what matters is the observed behaviour, reproducible on all three machines: after journald restarts, rsyslog keeps running but writes nothing.

That is the worst failure mode for logging: everything looks fine. Services are green, journald records correctly, journalctl shows recent entries. Only the text files are frozen, and with them everything that reads them: CrowdSec, fail2ban if it were there, logwatch, or any script doing a tail.

The check that doesn’t lie

To know whether a logging chain works, looking at service states isn’t enough. You have to push a message through end to end and check that it arrives. The logger command writes a message the way any program would; with -p authpriv.info, it’s filed under authentication, the category that feeds /var/log/secure.

# is anything still written? compare the files' age with now, then send a test line
date -r /var/log/secure +%H:%M:%S; date +%H:%M:%S
logger -p authpriv.info -t logcheck "rsyslog still writing?"
sleep 2; grep -c logcheck /var/log/secure

If the count stays at zero, rsyslog isn’t writing. Comparing the file’s modification time with the current time gives the same answer without writing anything, but on a quiet machine a file can legitimately stay unchanged for a few minutes; the test message removes the doubt.

The repair, and the gap filled

The repair is one command: restart rsyslog. On startup, imjournal rereads its state file, finds its cursor and resumes reading the journal where it stopped. Since journald itself had lost nothing, the seventy-five missing minutes were written back into the text files: 683 lines on the busiest machine. CrowdSec then read them in turn and caught up. No attack was missed, and that’s thanks to imjournal’s cursor.

systemctl restart rsyslog
# rsyslog resumes from the journal position saved in its imjournal state file:
# the 75 missing minutes are written into /var/log/messages and secure

Making sure it doesn’t happen again

A manual restart fixes the incident, not the cause. The next journald configuration change, on any machine, would do exactly the same thing. The lasting fix belongs in the Ansible role: whenever journald restarts, rsyslog must restart right after. Ansible handlers suit this well thanks to the listen keyword, which lets several handlers react to the same notification, in the order they’re written.

# roles/common/handlers/main.yml (excerpt)
- name: restart systemd-journald
  ansible.builtin.systemd:
    name: systemd-journald
    state: restarted

- name: flush journal to disk
  ansible.builtin.command: journalctl --flush
  listen: restart systemd-journald

- name: restart rsyslog after journald
  ansible.builtin.systemd:
    name: rsyslog
    state: restarted
  listen: restart systemd-journald

Order matters, and I learned it the same day. My first version ran journalctl --flush as an ordinary task, right after dropping the configuration file. But tasks run before handlers: the flush happened before journald restarted, under the old configuration, and achieved nothing. The /var/log/journal directory only appeared at the next restart. Making the flush a handler listening to the same notification as the restart guarantees it runs afterwards.

Two rows: first dropping the file, flushing the journal as a task under the old configuration, then restarting journald; then dropping the file, which notifies three handlers in order: restart journald, flush the journal, restart rsyslog
Tasks run before handlers: the flush and the rsyslog restart must follow the journald restart.

A change like this is proven on one machine before it’s rolled out. The simplest way to trigger the whole chain is to delete the configuration file: the role puts it back, which restarts journald and so fires the two handlers that follow. A test message then confirms rsyslog is still writing.

# prove the chain on one host: remove the drop-in, let the role put it back
rm /etc/systemd/journald.conf.d/00-retention.conf
ansible-playbook site.yml -l host --tags rsyslog
#   RUNNING HANDLER [common : restart systemd-journald]   changed
#   RUNNING HANDLER [common : flush journal to disk]      changed
#   RUNNING HANDLER [common : restart rsyslog after journald] changed
logger -p authpriv.info -t handlertest "after the role"; grep -c handlertest /var/log/secure

What I take away

Making the journal persistent is still an excellent idea, and I recommend it on any machine whose disk can take a few more writes. But restarting journald isn’t harmless: anything that consumes the journal can disconnect without saying so, and rsyslog is the most common example since it still feeds most tools that read text files.

The broader lesson is about monitoring. An active service isn’t a service doing its job. My monitoring didn’t see rsyslog fail, since it hadn’t; it saw that CrowdSec had read nothing for an hour, which is a business symptom, not a technical state. Those alerts, on what should arrive and no longer does, are the ones that catch silent failures. Without it, I would have found the problem days later, looking for an SSH login in a file that had stopped growing.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *