Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction of Logrotate Service under Linux system

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "introduction to Logrotate Services under the Linux system". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

For the daily management of linux, the log file is very important, it can see the point of the problem and related error information, but also can analyze the cause of the problem according to the information, which is one of the indispensable tools for the management system and service.

Logrotate is a very useful tool that automatically truncates (or rotates) logs, compresses, and deletes old log files. For example, you can set up logrotate to rotate the / var/log/XXX log file every 10 days and delete logs for more than a month. After configuration, the operation of logrotate is completely automated, which is actually the same as the system's scheduled task calling the custom script, and its operation is also a timing task to call its configuration file, so as to achieve the above effect.

The installation command is very simple (usually installed by default)

Yum install logrotate crontabs

Configuration file directory

/ etc/logrotate.d

# all configuration files that require this feature are stored in the directory

[root@centos logrotate.d] # cat zabbix-agent

/ var/log/zabbix/zabbix_agentd.log {

Daily

Rotate 12

Compress

Delaycompress

Missingok

Notifempty

Create 0664 zabbix zabbix

}

Parameter description

Daily # specifies the dump cycle as daily

Weekly # specifies the dump cycle as weekly

Monthly # specifies the dump cycle as monthly

Number of rotate 12 # dumps

Compress # logs after being dumped through gzip compression

Use this parameter when nocompress # does not need compression

Copytruncate

# for log files that are still open to back up and truncate the current log

Nocopytruncate

# back up log files but not truncate

Delaycompress

# when used with compress, the dumped log files will not be compressed until the next dump

Notifempty

# if the file is empty, it is not dumped

Postrotate/endscript

# commands that need to be executed after the dump can be put into this pair, and the two keywords must be on separate lines

Sharedscripts runs the postrotate script, which executes the script once after all the logs are rotated. If it is not configured, the script will be executed once after each log rotation

Size size

# the log file is not dumped until it reaches the specified size. Size can specify bytes (default) and KB (sizek) or MB (sizem).

Logrotate running process

1. The scheduled task of the system to run / etc/cron.daily/logrotate

[root@ ~] # cat / etc/cron.daily/logrotate

#! / bin/sh

/ usr/sbin/logrotate / etc/logrotate.conf

EXITVALUE=$?

If [$EXITVALUE! = 0]; then

/ usr/bin/logger-t logrotate "ALERT exited abnormally with [$EXITVALUE]"

Fi

Exit 0

2. Invoke the configuration file / etc/logrotate.conf by executing the / usr/sbin/logrotate command every day

[root@resources ~] # cat / etc/logrotate.conf

# see "man logrotate" for details

# rotate log files weekly

Weekly

# keep 4 weeks worth of backlogs

Rotate 4

# create new (empty) log files after rotating old ones

Create

# use date as a suffix of the rotated file

Dateext

# uncomment this if you want your log files compressed

# compress

# RPM packages drop log rotation information into this directory

Include / etc/logrotate.d

# no packages own wtmp and btmp-we'll rotate them here

/ var/log/wtmp {

Monthly

Create 0664 root utmp

Minsize 1M

Rotate 1

}

/ var/log/btmp {

Missingok

Monthly

Create 0600 root utmp

Rotate 1

}

# system-specific logs may be also be configured here.

3. Finally, call the configuration file under / etc/logrotate.d to perform relevant operations.

[root@ logrotate.d] # cat vsftpd

/ var/log/vsftpd.log {

# ftpd doesn't handle SIGHUP properly

Nocompress

Missingok

}

/ var/log/xferlog {

# ftpd doesn't handle SIGHUP properly

Nocompress

Missingok

}

It should be noted that when / etc/logrotate.conf reads the file, the parameters in the file specified by include override the default parameters

Sharing of logrotate production examples

_ share an example of cutting nginx logs _

Vim / etc/logrotate.d/nginx

# #

# for cut nginx log everyday

# create by mingongge at 2017-05-31

# #

/ var/log/nginx/*.log {

Daily

Rotate 7

Missingok

Notifempty

Dateext

Sharedscripts

Postrotate

If [- f / usr/local/nginx/logs/nginx.pid]; then

Kill-USR1 `cat / usr/local/nginx/logs/ nginx.pid`

Fi

Endscript

}

The effect of the two configurations is the same. Please refer to the on-demand configuration.

Vim / servser/scripts/auto_cut_nginxlog.sh

#! / bin/sh

# #

# this scripts for auto cut nginx log everyday

# create by mingongge at 2017-05-31

# #

LOGDIR=/var/log/nginx/

DATE= `date +% F`

Mv $LOGDIR/access.log $LOGDIR/nginx_access.$ {DATE} .log

Mv $LOGDIR/error.log $LOGDIR/nginx_error.$ {DATE} .log

Kill-USR1 `cat / var/run/ nginx.pid`

Cd $LOGDIR

Find. /-name "* .log"-mtime + 30 | xargs-I mv {} / tmp/

# in the actual production environment, people should be careful to use the rm command, or it is best to disable this command and use mv instead.

This is the end of the introduction of Logrotate Services under the Linux system. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report