Part of Datorkirurgen AB – IT experts across Sweden

A full disk and lack of memory bring down many Linux servers. This is how we limit journald, add swap and grow the disk without downtime on AWS and other clouds.

When the server suddenly stops responding

Two of our own servers on AWS started showing recurring operational disruptions one year. The websites stopped responding, SSH was slow and sometimes the server needed a restart. The cause turned out to be a combination of three things. The disk was almost full, the memory ran out because the server had no swap, and the system log had grown enormous. Each problem alone was manageable, but together they brought down the server.

This article describes how we solved it. The screenshots come from the same work. The solution was not advanced, but it required us to fix all parts at the same time. Therefore, today we have the same basic settings on all servers we operate.

At first, the problems appeared as short interruptions of a few minutes, often at night. Therefore, it took time before anyone connected them. In addition, the logs showed different faults each time, since the kernel stopped different processes when the memory ran out. Only when we looked at disk, memory and logs at the same time did the pattern become clear. That is an important lesson: with recurring faults, look at the whole picture and not at a single message.

Journald frees 838 MB and df shows that the disk was 87 per cent full
Journald frees 838 MB and df shows that the disk was 87 per cent full. A real screenshot from our server. The host name is masked.

Journald: the log that ate the disk

Systemd collects logs in journald. It is practical, but without a limit the log can grow until the disk is full. On our server, it took almost a gigabyte. We started by cleaning up with journalctl --vacuum-size, which removes older logs until the logs reach the limit. After that, we set a permanent limit in a separate configuration file, so that the same thing cannot happen again.

We chose 100 to 200 MB depending on the size of the server. It is enough to troubleshoot the last few weeks. However, if you need to keep logs for longer, we instead send them to a central log server or to CloudWatch. In addition, we check that logrotate works for Apache and other services that write their own log files. Otherwise, they grow in the same way. For example, we often see Apache logs of several gigabytes on servers that nobody has touched for a few years.

A permanent limit for journald in a separate configuration file
A permanent limit for journald in a separate configuration file. Do you need help with Linux? Linux Service helps you in Stockholm, on site or remotely.

Swap: a safety margin for the memory

Many virtual servers in the cloud lack swap from the start. As a result, the server has no margin when the memory runs out. Then the kernel starts terminating processes, and often the database stops first. Therefore, we added a swap file of 2 to 4 GB. It does not make the server faster, but it prevents a crash during a temporary peak.

In addition, we set vm.swappiness to a low value. Then the system only uses swap when the memory really runs out, and as a result it does not affect performance in daily use. With top and free, we then follow how the server uses memory. If the server uses swap a lot all the time, it is a sign that it needs more memory. In that case, we propose a larger instance instead.

Top shows memory, swap and load after adding swap
Top shows memory, swap and load after we have added swap. An example from our work for clients in Vasastan and Norrmalm.
Basic settings for journald, swap and disk on all servers we operate
Basic settings for journald, swap and disk on all servers we operate. Linux Service, part of Datorkirurgen AB, helps businesses in Gothenburg and Malmö with servers, scripts and databases.

Growing the disk without interruption

When the disk became too small anyway, we enlarged it. In AWS, you change the size of the EBS volume in the console, and after that the operating system sees the new space. The partition grows with growpart, and the file system with xfs_growfs or resize2fs. Everything happens while the server is running, and therefore visitors notice nothing.

We always take a snapshot before we change the size. Things rarely go wrong, but the consequences can be large. In addition, we add a clean-up script that starts twice a month. It removes temporary files, old logs and the package manager’s cache, as well as old PHP sessions. Read more about Bash scripts for such automation. Finally, we set up an alert when the disk reaches 80 per cent, so that we always have time to act.

Growing the disk with growpart and xfs_growfs while the server is running
Growing the disk with growpart and xfs_growfs while the server is running. This is how Linux Service, part of Datorkirurgen AB, works for businesses in Uppsala.

Contact Linux Service about journald and disk space

Linux Service is part of Datorkirurgen AB and operates Linux servers in AWS, Azure, Google Cloud and own data centres. We set up journald, swap and monitoring, and we grow disks without interruption. The systemd documentation describes the settings for journald.

Contact us if your server shows operational disruptions or if the disk is approaching full. We carry out a quick review and fix what is causing trouble. We work remotely. Moreover, we can take over the ongoing operation, so that it does not happen again.

Frequently asked questions about journald

Questions about journald

Journald, swap and memory

More about disks and support

Website developed by Datorkirurgen AB · Privacy policy