Here is an example logrotate.conf file:
# specify the location of the log files
/var/log/messages {
# rotate the log files every day
daily
# keep 30 rotated copies of the log files
rotate 30
# compress the rotated log files
compress
# delay compression of the rotated log files until the next rotation
delaycompress
# create a new log file after rotating the old one
create
# set the owner and group of the new log file
owner root
group root
# set the permissions of the new log file
mode 0644
}
# specify the location of another set of log files
/var/log/nginx/*.log {
# rotate the log files when they reach a size of 100MB
size 100M
# keep 3 rotated copies of the log files
rotate 3
# compress the rotated log files
compress
# delay compression of the rotated log files until the next rotation
delaycompress
# create a new log file after rotating the old one
create
# set the owner and group of the new log file
owner root
group root
# set the permissions of the new log file
mode 0644
}
In this example, the first block specifies that the log files in the /var/log/messages directory should be rotated daily, with 30 rotated copies kept and compressed.
The second block specifies that log files in the /var/log/nginx directory with a .log extension should be rotated when they reach a size of 100MB, with 3 rotated copies kept and compressed.
You can also use logrotate to
specify a script to be run before or after the log files are rotated. This can
be useful for tasks such as restarting services or sending notifications when
log rotation occurs.
and the majority of the commonly used directives are listed below:
1.
compress: This directive
specifies whether the rotated log files should be compressed to save space.
2.
compresscmd: This directive
specifies the command to use for compressing rotated log files.
3.
compressext: This directive
specifies the extension to use for compressed log files.
4.
compressoptions: This
directive specifies options to pass to the compression command.
5.
copy: This directive
specifies that the log file should be copied, rather than moved, when it is
rotated.
6.
copytruncate: This
directive specifies that the log file should be truncated (emptied) after it is
copied for rotation.
7.
create: This directive
specifies the permissions and ownership of the rotated log files.
8.
dateext: This directive
specifies that a date stamp should be added to the rotated log file names.
9.
dateformat: This directive
specifies the format of the date stamp that is added to rotated log file names.
10.
delaycompress: This
directive specifies whether the rotated log files should be compressed, but not
until the next rotation.
11.
ifempty: This directive
specifies whether log rotation should occur even if the log file is empty.
12.
include: This directive
specifies additional configuration files to be included in the current
configuration.
13.
mail: This directive
specifies an email address to which a message should be sent after log
rotation.
14.
mailfirst: This directive specifies
that a message should be sent before log rotation.
15.
maillast: This directive
specifies that a message should be sent after log rotation.
16.
maxage: This directive
specifies the maximum number of days that a rotated log file should be kept.
17.
missingok: This directive
specifies that logrotate should continue even if the log file does not exist.
18.
monthly: This directive
specifies that log rotation should occur on a monthly basis.
19.
nocompress: This directive
specifies that rotated log files should not be compressed.
20.
nocopy: This directive
specifies that the log file should be moved, rather than copied, when it is
rotated.
21.
nocopytruncate: This
directive specifies that the log file should not be truncated after it is
copied for rotation.
22.
nomail: This directive
specifies that no message should be sent after log rotation.
23.
noolddir: This directive
specifies that rotated log files should not be placed in a separate directory.
24.
nosharedscripts: This
directive specifies that the postrotate script should not be run for each
rotated log file.
25.
notifempty: This directive
specifies that log rotation should not occur if the log file is empty.
26.
postrotate: This directive
specifies a script to be run after log rotation.
27.
prerotate: This directive
specifies a script to be run before log rotation.
28.
rotate: This directive
specifies the number of rotated log files that should be kept.
29.
sharedscripts: This
directive specifies that the postrotate script should be run for each rotated
log file.
30.
size: This directive
specifies the maximum size that a log file should reach before it is rotated.
31. weekly: This directive specifies that log rotation should occur on a weekly basis.
lastaction keyword
It is a keyword that is used in the configuration file for the `logrotate` utility.Here is an example of how the `lastaction` keyword might be used in a logrotate configuration file:
/var/log/myapp.log { rotate 7 compress delaycompress copytruncate lastaction /usr/local/bin/myapp-log-cleanup endscript }
In this example, the lastaction keyword specifies that the script /usr/local/bin/myapp-log-cleanup should be run after the log file /var/log/myapp.log has been rotated. The script specified with the lastaction keyword can perform any desired actions, such as deleting old log files or sending an email notification.
you can get full list of commands used in logroate configuration file by checking the man Page: man logrotate
Comments
Post a Comment
Leave a comment to encourage us, share your thoughts on the subject, or propose a new one.