How to Install and Configure Logrotate On Alpine Linux

This post will guide you how to install and configure Logrotate On a Alpine Linux. How do I setup Logrotate to rotate and archive any log files on your Alpine Linux Docker VM.

install logrotate alpine 1

What is Logrotate?


logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

Normally, logrotate is run as a daily cron job. It will not modify a log more than once in one day unless the criterion for that log is based on the log’s size and logrotate is being run more than once each day, or unless the -f or –force option is used.

The syntax of the logrotate command is as followss:

logrotate [-dv] [-f|--force] [-s|--state file] config_file ..

Installing Logrotate


You need to type the following apk command to install Logrotate package in your Alpine Linux:

# apk add logrotate

Outputs:

/ # apk add logrotate
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
(1/3) Installing popt (1.16-r7)
(2/3) Installing logrotate (3.15.0-r0)
(3/3) Installing logrotate-openrc (3.15.0-r0)
Executing busybox-1.30.1-r2.trigger
OK: 23 MiB in 28 packages

Configuring Logrotate


Once Logrotate tool is installed on your Linux system, and you can use it to manage a specific log file. such as: you have a log file called log-file is located in the /var/log/ directory.

The configuration file of the Logrotate is /etc/logrotate.conf file. Normally, you do not need to modify it.

let’s create a log file called log-file under /var/log/ directory with the following command:

$ touch /var/log/log-file

And you can write 10MB random data into this log file using the following command:

# head -c 10m </dev/urandom > /var/log/log-file

Then you need to create a config file for this log file under the /etc/logrotate.d/ direcotry,type:

# vi /etc/logrotate.d/log-file

Adding the following lines into the file

/var/log/log-file {
monthly
rotate 5
compress
delaycompress
missingok
notifempty
create 644 root root
postrotate
/usr/bin/killall -HUP rsyslogd
endscript
}

Save and close the file.

the log-file will be rotated monthly by Logrotate tool.

You can also run logrotate by manually with the following command:

# logrotate -vf /etc/logrotate.d/log-file

Outputs:

/ # logrotate -vf /etc/logrotate.d/log-file
reading config file /etc/logrotate.d/log-file
Reading state from file: /var/lib/logrotate.status
Allocating hash table for state file, size 64 entries

Handling 1 logs

rotating pattern: /var/log/log-file forced from command line (5 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/log-file
Creating new state
Now: 2019-09-16 11:46
Last rotated at 2019-09-16 11:00
log needs rotating
rotating log /var/log/log-file, log->rotateCount is 5
dateext suffix '-20190916'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
previous log /var/log/log-file.1 does not exist
renaming /var/log/log-file.5.gz to /var/log/log-file.6.gz (rotatecount 5, logstart 1, i 5),
old log /var/log/log-file.5.gz does not exist
renaming /var/log/log-file.4.gz to /var/log/log-file.5.gz (rotatecount 5, logstart 1, i 4),
old log /var/log/log-file.4.gz does not exist
renaming /var/log/log-file.3.gz to /var/log/log-file.4.gz (rotatecount 5, logstart 1, i 3),
old log /var/log/log-file.3.gz does not exist
renaming /var/log/log-file.2.gz to /var/log/log-file.3.gz (rotatecount 5, logstart 1, i 2),
old log /var/log/log-file.2.gz does not exist
renaming /var/log/log-file.1.gz to /var/log/log-file.2.gz (rotatecount 5, logstart 1, i 1),
old log /var/log/log-file.1.gz does not exist
renaming /var/log/log-file.0.gz to /var/log/log-file.1.gz (rotatecount 5, logstart 1, i 0),
old log /var/log/log-file.0.gz does not exist
log /var/log/log-file.6.gz doesn't exist -- won't try to dispose of it
renaming /var/log/log-file to /var/log/log-file.1
creating new /var/log/log-file mode = 0644 uid = 0 gid = 0
running postrotate script
killall: rsyslogd: no process killed
error: error running non-shared postrotate script for /var/log/log-file of '/var/log/log-file '

If you want to run all configuration file under /etc/logrotate.d/ directory, and you can use the following command:

#logrotate /etc/logrotate.conf

Conclusion


You should know that how to install and configurate Logrotate to manage log files in your Alpine Linux.

You might also like:

Sidebar



back to top