How to Set or Change User Agent with curl
Curl is a valuable tool for web development and testing...
A log file that grows without a retention policy can fill a VPS disk. On Ubuntu, logrotate can rename old files, compress archives and remove older rotations according to the rules you set. This guide walks through one application log and uses a separate test before you apply the rule.
You need sudo access, the path to the log file and the user and group that should own it. Use the application’s documentation to check how it writes and reopens logs. If the app logs only to the systemd journal, manage journal retention separately.
logrotate --version
sudo ls -l /etc/logrotate.d/
sudo ls -l /var/log/myapp/application.log
If logrotate is missing, install Ubuntu’s package. Check /etc/logrotate.conf for the include directive and look for an existing rule that matches your log path.
sudo apt update
sudo apt install logrotate
sudo less /etc/logrotate.conf
This example uses an application called myapp. Replace the path, user and group with the values from your server. The directory permissions must allow the specified account to rename the log and create the replacement. Ask the application to reopen the log after it rotates, using its documented reload or reopen procedure.
sudoedit /etc/logrotate.d/myapp
/var/log/myapp/application.log {
su myapp myapp
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 0640 myapp myapp
}
The rule keeps up to seven rotated copies. Missing logs and empty logs are skipped. The newest rotated copy stays uncompressed until the next rotation. If the app keeps writing to the renamed file, the create directive alone cannot redirect it. Add the application’s documented reopen command in a postrotate block where supported.
If the application cannot reopen its logs, copytruncate is a fallback. A small amount of logging can be lost between copying and truncating the file, and create has no effect with copytruncate. Review the Ubuntu logrotate manual before choosing it.
Use daily for a daily check or size for size-based rotation. To combine daily rotation with an earlier size trigger, use daily and maxsize in the same rule. Thresholds are evaluated when logrotate runs, so a large log can exceed its configured threshold between runs.
Run debug mode against the main configuration. It checks the full set of rules without writing the logrotate state file or rotating logs. Read the output for duplicate entries, permission problems or invalid directives.
sudo logrotate -d /etc/logrotate.conf
For a rotation test, make a temporary directory as your regular user. Keep this configuration outside /etc/logrotate.d. The test uses a separate state file and touches only the temporary log.
test_dir="$(mktemp -d /var/tmp/logrotate-demo.XXXXXX)"
printf '%s\n' 'Test entry before rotation' > "$test_dir/app.log"
cat > "$test_dir/logrotate.conf" <<CONFIG
$test_dir/app.log {
size 1k
rotate 2
compress
missingok
notifempty
create 0600
}
CONFIG
logrotate -d -s "$test_dir/state" "$test_dir/logrotate.conf"
logrotate -v -f -s "$test_dir/state" "$test_dir/logrotate.conf"
ls -l "$test_dir"
gzip -cd "$test_dir/app.log.1.gz"
After the test, check that app.log exists and that reading app.log.1.gz prints the original entry. The forced rotation is useful here because the temporary log is smaller than the size threshold.
On an Ubuntu host using systemd, check the timer and service journal. A successful one-shot service can be inactive between runs. If your installation uses cron, inspect the job and its logs. Make sure only the intended scheduler runs the production policy.
systemctl list-timers --all logrotate.timer
systemctl status logrotate.timer
sudo journalctl -u logrotate.service --since "7 days ago"
After the first scheduled rotation, check file permissions and confirm that new application entries appear in the replacement log. If the old file continues to grow, review the reopen step. If rotation is skipped, check the size or time condition, the last rotation time and the rule’s permissions.
Use daily for a time-based policy or size for size-only checks. Combine daily with maxsize when you also want a size threshold at each check. The scheduler determines when logrotate checks the rule.
Add compress to gzip archived logs. With delaycompress, the most recent archive is left uncompressed until the next rotation.
On a systemd host, list logrotate.timer and check the journal for logrotate.service. On a host that uses cron, check its scheduled job and logging instead.
Use -d first. Run a forced rotation only against a disposable log with an isolated configuration and state file, or during a controlled service test you have planned.
Check retention requirements and which process owns the file first. Removing an active log may not release disk space immediately if a process still has it open.