The short answer: logrotate is the program that, every day, takes the log files, keeps the old ones compressed and starts a new one, deleting the oldest. The system services already come configured. Yours (an application, a script, a container) do not, and it is almost always one of those that fills the disk. A configuration file of half a dozen lines is enough.
On a VPS with root access, log housekeeping is yours. When the disk fills, the site and the database stop writing and strange errors begin. The symptom and the first step are in No space left on device.
First, find the fat file
| 2 |
See what the system journal takes: journalctl --disk-usage. To shrink it, sudo journalctl --vacuum-size=200M keeps only the most recent entries up to that size.
|
|
How to rotate a log of your own
Create a file in /etc/logrotate.d/ named after the application, for example /etc/logrotate.d/my-app, with this content (changing the path to that of your logs):
/var/log/my-app/*.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
copytruncate
}
| Line |
What it does |
| weekly |
Rotates once a week. daily and monthly also exist. To rotate by size, use size 50M. |
| rotate 4 |
Keeps four old files and deletes the oldest. |
| compress and delaycompress |
Compresses the old ones, leaving the latest uncompressed for a program that may still be reading it. |
| missingok and notifempty |
No error if the file is missing, and an empty file is not rotated. |
| copytruncate |
Copies the log and empties the original, instead of renaming it. For programs that keep the file open and cannot open a new one. |
Test before you trust it
| 1 |
A dry run that changes nothing: sudo logrotate -d /etc/logrotate.d/my-app. The -d shows what it would do, without doing it.
|
|
| 2 |
Force a rotation now: sudo logrotate -f /etc/logrotate.d/my-app. Then look at the folder: there should be a new file and a compressed one.
|
|
| 3 |
Let it run: the system calls logrotate once a day, by itself. You do not need to create a cron job for it.
|
|
|
Do not delete with rm a log that a program has open. The file disappears from the folder, but the space only comes back when the program closes it, and the disk stays full. To empty it without deleting use sudo truncate -s 0 /path/to/log.
|
|
For a container, the right limit is in Docker’s own configuration: the maximum size and the number of files of each container’s log. See Running Docker on a VPS safely. The commands in this article are yours: our support does not write them or repair them (see How far our support goes).
|
|
The VPS disk fills up and you cannot tell where from? Open a ticket with the output of the first command.
Open a support ticket
|
RECOMMENDED PRODUCT VPS server with root access Resources of your own, the OS you choose, reinstall whenever you like. from 456,30 MT/mo (3-year plan, with coupon) See plans |