Data and monitoring · Beginner · 18 min
How to Stop Log Files Filling Your Disk with logrotate
By Padding · Updated 16 Nov 2026
Ubuntu 26.04.1 LTS · logrotate 3.22.0
You will write a logrotate 3.22 rule for an application's log on Ubuntu 26.04, rotate it by size or age, compress and expire old copies, and fix the classic problem where a program keeps writing to the rotated file, using copytruncate and a signal to reopen the log.
On this page
Programs write logs forever. Nothing deletes them, so a busy service can fill a disk and take the server down with it. That is a common reason a healthy server stops. logrotate solves it. On a schedule it renames the current log to an older name, starts a fresh one, compresses the old copies and deletes the oldest. The disk use of a log then stays bounded.
Ubuntu installs logrotate and already rotates its own logs and those of most packages. This tutorial is about your own logs: an application you wrote or installed by hand, whose log nothing rotates yet. You create a demo program that writes a log, write a rotation rule, test it without risk, and then hit and fix the one trap that catches almost everyone: a program that keeps writing to a file after logrotate has renamed it.
Step 1 · See how logrotate already runs
Install it if it is missing, and look at the main configuration:
sudo apt update
sudo apt install -y logrotate
logrotate --version 2>&1 | head -n 1
grep -v '^#' /etc/logrotate.conf | grep -v '^$'
ls /etc/logrotate.dThe version line is logrotate 3.22.0 on Ubuntu 26.04. The settings in /etc/logrotate.conf are the defaults for every log: on Ubuntu weekly, su root adm, rotate 4 (keep four old copies) and create (make a new empty file after rotating), followed by include /etc/logrotate.d. That last line loads one small file per application from /etc/logrotate.d/. Your rule goes there.
On Ubuntu a systemd timer starts logrotate once a day. You can see it with systemctl list-timers logrotate.timer, and the rule is in /usr/lib/systemd/system/logrotate.timer. The timer adds a random delay of up to an hour, so rotation does not happen at a fixed minute. That matters later: a log that grows faster than once a day needs more than the size rules below.
Step 2 · Create a demo application and its log
The demo program stands in for your application. It writes a line every fifth of a second to /var/log/demoapp/app.log, and it keeps the file open, the way most servers do. First create a user for it and a folder for its logs. The user has no login, as for any service:
sudo useradd --system --no-create-home --shell /usr/sbin/nologin demoapp
sudo install -d -o demoapp -g demoapp -m 0750 /var/log/demoapp
sudo install -d -m 0755 /opt/demoappWrite the program. It writes its process ID to app.pid next to the log, the way a real service writes a PID file. It takes an optional argument, reopen, which makes it close and reopen its log when it receives the SIGHUP signal. You will use that in the last step:
sudo tee /opt/demoapp/app.py > /dev/null <<'PY'
import os
import signal
import sys
import time
PATH = "/var/log/demoapp/app.log"
log = open(PATH, "a", buffering=1)
with open("/var/log/demoapp/app.pid", "w") as pidfile:
pidfile.write(str(os.getpid()))
def reopen(*_):
global log
log.close()
log = open(PATH, "a", buffering=1)
if len(sys.argv) > 1 and sys.argv[1] == "reopen":
signal.signal(signal.SIGHUP, reopen)
n = 0
while True:
n += 1
log.write(f"{time.strftime('%H:%M:%S')} request {n}\n")
time.sleep(0.2)
PYStart it as the demoapp user in a second terminal, and leave it running:
sudo -u demoapp python3 /opt/demoapp/app.pyCheck it worked
sudo tail -n 3 /var/log/demoapp/app.logYou see three lines such as 14:02:11 request 41, and the number grows every time you run it.
Step 3 · Write a rotation rule
Create a file in /etc/logrotate.d/. The name does not matter, but a clear one helps. Each rule starts with the log path and has its options in braces:
sudo tee /etc/logrotate.d/demoapp > /dev/null <<'CONF'
/var/log/demoapp/app.log {
daily
rotate 7
maxsize 50M
missingok
notifempty
compress
delaycompress
create 0640 demoapp demoapp
}
CONF
sudo chmod 644 /etc/logrotate.d/demoappWhat each option does:
dailyrotates once a day when logrotate runs.weeklyandmonthlyalso exist.rotate 7keeps seven old copies. The oldest is deleted at the eighth rotation, so this log never takes more than eight files of space.maxsize 50Malso rotates when the file is above 50 MB at the next run, even if a day has not passed. It only acts when logrotate runs, which is once a day by default.missingokis not an error if the log file does not exist.notifemptyskips an empty log.compressgzips old copies.delaycompressleaves the newest old copy (app.log.1) uncompressed until the next rotation, which helps programs that are still finishing a write to it. Text logs shrink by roughly ten times.create 0640 demoapp demoappmakes the new file with that mode and owner, so the program can write to it and nobody else can read it. Without it the new file would belong to root, and the program could not write to it.
Never edit the files in /etc/logrotate.d so that they are writable by anyone but root. logrotate runs as root and refuses files with unsafe modes, because anything it reads can run commands.
Step 4 · Test the rule without changing anything
--debug (or -d) reads the configuration and prints what logrotate would do, without touching a file. It is the first thing to run after any change:
sudo logrotate --debug /etc/logrotate.d/demoapp 2>&1 | head -n 30The output is long. Read for reading config file /etc/logrotate.d/demoapp, the pattern /var/log/demoapp/app.log, and a line such as log does not need rotating (log has been rotated at ...) or log needs rotating. A syntax error appears here as error: with the line number. Today the log is new and under 50 MB, so no rotation is due.
Step 5 · Force a rotation and look at the result
Make logrotate rotate now, whatever the schedule says. --force (or -f) is how you test the real effect of a rule:
sudo logrotate --force /etc/logrotate.d/demoapp
sudo ls -l /var/log/demoapp/You see the new app.log, owned by demoapp with mode -rw-r-----, and app.log.1, which holds the earlier lines and is not yet compressed because of delaycompress. The app.pid file is the demo program's PID file, which the rule does not touch. Force a second rotation a few seconds later:
sleep 2
sudo logrotate --force /etc/logrotate.d/demoapp
sudo ls /var/log/demoapp/The list is the same as before, and no app.log.2.gz has appeared. Even --force obeys notifempty, and the new app.log is empty, so there was nothing to rotate. That is a clue, and the next step explains why the file is empty. logrotate remembers when it last rotated each log in /var/lib/logrotate/status, which is why a forced rotation does not break the normal schedule.
Step 6 · Find the trap: the program keeps the old file
Look at the new app.log closely. The program is still running. Check which file the new lines go to:
sleep 2
echo "app.log:"; sudo wc -l /var/log/demoapp/app.log
echo "app.log.1:"; sudo wc -l /var/log/demoapp/app.log.1
sleep 3
echo "after 3 seconds:"
echo "app.log:"; sudo wc -l /var/log/demoapp/app.log
echo "app.log.1:"; sudo wc -l /var/log/demoapp/app.log.1wc -l prints the line count followed by the file name. app.log stays at 0 lines, while app.log.1 keeps growing. This is the trap. A running program holds its log open by file descriptor, which points at the file's data and not at its name. When logrotate renames app.log to app.log.1, the program carries on writing to the same data, now called app.log.1. The new app.log that create made stays empty. The log you look at is silent, and the file that keeps growing will be compressed and deleted in a few days.
There are two fixes. Try the simpler one first.
Step 7 · Fix 1: copytruncate
copytruncate changes the method. Instead of renaming the file, logrotate copies its contents to app.log.1 and then empties the original in place. The program's file descriptor still points at the original file, so it carries on writing to app.log.
First, put the demo back in a clean state. The program is currently writing to a file that has already been rotated away, so restart it. It then opens the current app.log again. Stop it with Ctrl+C in its terminal (or with the kill line below) and start it again:
sudo kill "$(sudo cat /var/log/demoapp/app.pid)"
sudo -u demoapp python3 /opt/demoapp/app.pyNow write the rule with copytruncate and rotate:
sudo tee /etc/logrotate.d/demoapp > /dev/null <<'CONF'
/var/log/demoapp/app.log {
daily
rotate 7
maxsize 50M
missingok
notifempty
compress
delaycompress
copytruncate
}
CONF
sudo logrotate --force /etc/logrotate.d/demoapp
sleep 3
echo "app.log:"; sudo wc -l /var/log/demoapp/app.logThe count is now above zero and keeps growing. create is gone, because copytruncate keeps the original file, so there is nothing new to create. The cost: lines the program writes between the copy and the truncate are lost, and the copy needs disk space for the whole file. For a log that must never lose a line, use the next fix.
Step 8 · Fix 2: tell the program to reopen its log
The cleaner fix is to rename the file as normal, then send the program a signal so it opens the new file. Most servers support this: Nginx reopens its logs on USR1, many daemons on HUP, and some programs have a reload command. Check your program's documentation for the right one. The demo program reopens on HUP when started with reopen. Stop it with Ctrl+C in its terminal, or with the kill line below, and start it again with the argument:
sudo kill "$(sudo cat /var/log/demoapp/app.pid)"
sudo -u demoapp python3 /opt/demoapp/app.py reopenRun the second command from another terminal if the first still occupies one. Now write the rule with a postrotate script. sharedscripts runs it once per rule, however many logs the rule matches. The commands between postrotate and endscript run after the files are rotated:
sudo tee /etc/logrotate.d/demoapp > /dev/null <<'CONF'
/var/log/demoapp/app.log {
daily
rotate 7
maxsize 50M
missingok
notifempty
compress
delaycompress
create 0640 demoapp demoapp
sharedscripts
postrotate
kill -HUP "$(cat /var/log/demoapp/app.pid)"
endscript
}
CONF
sudo logrotate --force /etc/logrotate.d/demoapp
sleep 3
echo "app.log:"; sudo wc -l /var/log/demoapp/app.log
sudo ls -l /var/log/demoapp/app.logapp.log is a fresh file with the owner and mode from create, and it receives new lines. In a real service the script is usually systemctl reload myapp or kill -USR1 $(cat /run/myapp.pid). Avoid pkill -f here: logrotate runs the script through a shell whose command line contains the same text, and pkill -f can signal that shell and make the rotation fail. Never run a rotation script that restarts a busy service every night if a reload will do.
Troubleshooting
error: skipping "..." because parent directory has insecure permissions: the log's folder is writable by a group or by everyone, and logrotate refuses to act as root there. Fix the folder's permissions, or addsu <user> <group>to the rule to rotate as the folder's owner.- Logs are never rotated: check that the file in
/etc/logrotate.dhas a valid path and ends in a closing brace, runsudo logrotate --debug /etc/logrotate.conf, and look atsystemctl list-timers logrotate.timerto see that the timer is active. error: ... permission deniedinpostrotate: the script runs as root unless you setsu. Run the command by hand to see its error.- Your log files vanish:
rotate 0deletes old copies at once. Use a number of at least 1. - A file keeps growing anyway: the program still writes to the old file. Redo the check in the trap step.
Step 9 · Check the real schedule and clean up
You are done after the last step: the timer runs logrotate /etc/logrotate.conf daily, which includes your file. To confirm it will run:
systemctl list-timers logrotate.timerThis lists the next run time. To rotate more often than daily, for example for a very busy log, give logrotate more than one chance a day: run sudo systemctl edit logrotate.timer and add these lines in the editor that opens. The empty OnCalendar= first clears the packaged schedule, and without it the timer would run on both:
[Timer]
OnCalendar=
OnCalendar=hourlyThen add hourly to the rule, or use maxsize, which acts at each run.
Remove the demo. Stop the program, delete the rule, the files and the user:
sudo kill "$(sudo cat /var/log/demoapp/app.pid)"
sudo rm /etc/logrotate.d/demoapp
sudo rm -r /var/log/demoapp /opt/demoapp
sudo userdel demoappThe state file /var/lib/logrotate/status keeps a line for the removed log. It is harmless and logrotate drops it on its own.
Next steps
- Find out where your disk space is going: find what is using disk space.
- Run your own jobs on a schedule: schedule jobs with cron.
- Run your app as a proper service: run your app as a systemd service.
- Get an alert before a disk fills: send alerts with Prometheus Alertmanager.
Related tutorials
- How to Find and Free Up Disk Space on a Linux ServerYou will find which folders and files fill a Linux disk with df and du, free space safely from logs and packages, and track down space held by deleted files.
- How to Schedule Jobs with Cron on LinuxYou will schedule a command to run every minute, then on a real timetable, read the cron syntax, and learn why a job that works in your shell can fail under cron.
- How to Run Your App as a systemd Service on Ubuntu 26.04You will write a systemd unit file that starts your app at boot, restarts it when it crashes, runs it as an unprivileged user, and sends its logs to the journal.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials