CUSTOM LOGROTATE RULES ALONGSIDE SYSTEM-WIDE CONFIGURATION
We frequently use logrotate to control log rotation on Linux servers. By default,
logrotate use the custom configuration files locate in
But what if we need a specific log file to be handled separately from the system-wide log rotation?
In this case, we can create a custom logrotate configuration file outside the
In this new file, specify the log file, how frequently you want it to rotate, how many rotated versions to keep, and any other specific settings:
Save the file and exit the text editor.
logrotate configuration file, you can use various options to customize the behaviour of log rotation. Here are some of the common options:
compress: This option compresses the rotated log files using gzip or other compress utility. It is used to save disk space.
copytruncate: This option allows
logrotateto truncate the original log file after creating a copy, instead of moving or deleting it. This is useful for applications that do not support log file rotation.
create: This option creates a new empty log file after rotation, if it does not exist. You can specify the permissions and owner/group for the new file.
daily: Rotates the log file once a day. This is one of the frequency options. Other options include
dateext: Appends a date in
YYYYMMDDformat to the rotated log files. This is useful for keeping track of when each log was created.
delaycompress: Postpones the compression of log files until the next rotation. This can be useful in conjunction with
copytruncateto prevent race conditions.
ifempty: Rotates the log file even if it is empty. By default,
logrotateskips empty files.
missingok: Ignores errors if the log file is missing.
notifempty: Prevents log rotation if the log file is empty.
rotate n: Specifies how many rotated versions of the log file to keep. For example,
rotate 4will keep four rotated log files.
size: Rotates the log file when it reaches a specified size. You can use suffixes like
Mfor megabytes, etc. For example, size
10Mmeans rotate when the file reaches 10 megabytes.
sharedscripts: Runs the
postrotatescript only once for all rotated logs, rather than once for each log.
su: Specifies the user and group ownership of the log file after rotation. This can be used to change ownership if log files are created with elevated privileges.
prerotate: These options allow you to specify shell commands or scripts to be run before or after log rotation. For example, you could use
postrotateto reload a service after log rotation.
lastaction: This is similar to
prerotatebut is run after all log files have been rotated.
These are some common options you can use in a
logrotate configuration file. Depending on your specific needs, you may use a combination of these options to achieve the desired log rotation behaviour.
Check the syntax of your configuration file:
-vdf stands for:
v: produce a detailed verbose output
d: execute a dry-run, in other words, a simulation of which
f: force the rotation even if the defined criteria are not met
Open the crontab configuration for editing:
Add a line to schedule the execution of your custom log rotation at the desired time. For example, if you want to run it every day at 7 PM, add the following line:
Save and exit the editor.
With this setup, your custom log rotation for the /apps/app_name/logs/log_name.log file will occur at the time specified in the
cron job, and it won’t be affected by the system-wide log rotation.