14.1.1. 日志

14.1.1.1. 日志切割

sudo apt-get install logrotate  # Debian/Ubuntu 系统
sudo yum install logrotate      # CentOS/RHEL 系统
vim /etc/logrotate.d/nginx
/var/log/nginx/*.log {
        missingok # 如果日志文件丢失,不会报错
        sharedscripts # 确保在切割后执行 postrotate 脚本
        postrotate
                /etc/init.d/nginx --quiet --ifstarted reopen
        endscript
}
  • –quiet: 这个参数使得 Nginx 命令在执行时不会输出任何不必要的消息,仅在出现错误时才会输出错误信息。

  • –ifstarted: 该选项表示如果 Nginx 已经启动,才会执行后续的操作。如果 Nginx 没有启动,则不会执行任何操作。这样可以避免在 Nginx 没有运行时进行重载或重新打开操作。

  • reopen: 这个命令用于让 Nginx 重新打开其日志文件。通常,这在日志文件轮转(log rotation)之后使用,因为 Nginx 在启动时会打开日志文件,但如果日志文件被替换或轮转,Nginx 并不会自动重新打开新的日志文件。使用 reopen 命令可以让 Nginx 重新打开日志文件。

再来一个例子:

 1/var/log/nginx/*.log {
 2    daily                 # 按天切割日志
 3    missingok             # 如果日志文件丢失,不会报错
 4    rotate 30             # 保留最近 30 天的日志
 5    compress              # 压缩旧日志
 6    delaycompress         # 延迟压缩,默认最新的日志文件不压缩
 7    notifempty            # 如果日志为空,不切割
 8    create 0644 www-data www-data  # 创建新的日志文件时的权限和属主
 9    sharedscripts         # 确保在切割后执行 postrotate 脚本
10    postrotate
11        # 重新加载 Nginx 配置,确保 Nginx 使用新创建的日志文件
12        /usr/sbin/nginx -s reopen
13    endscript
14}
# 可以通过以下命令手动测试 logrotate 配置是否生效:
sudo logrotate /etc/logrotate.d/nginx --debug

需要配置一个每日运行的crontab任务:

cat /etc/crontabs/root
# do daily/weekly/monthly maintenance
# min   hour    day     month   weekday command
*/15    *       *       *       *       run-parts /etc/periodic/15min
0       *       *       *       *       run-parts /etc/periodic/hourly
0       2       *       *       *       run-parts /etc/periodic/daily
0       3       *       *       6       run-parts /etc/periodic/weekly
0       5       1       *       *       run-parts /etc/periodic/monthly

注意/etc/periodic/daily文件中的内容:

$ls -l /etc/periodic/daily/
total 5
-rwxr-xr-x    1 root     root           367 Apr 11  2023 logrotate
 1$cat /etc/periodic/daily/logrotate
 2#!/bin/sh
 3
 4if [ -f /etc/conf.d/logrotate ]; then
 5        . /etc/conf.d/logrotate
 6fi
 7
 8if [ -x /usr/bin/cpulimit ] && [ -n "$CPULIMIT" ]; then
 9        _cpulimit="/usr/bin/cpulimit --limit=$CPULIMIT"
10fi
11
12$_cpulimit /usr/sbin/logrotate /etc/logrotate.conf
13EXITVALUE=$?
14if [ $EXITVALUE != 0 ]; then
15    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
16fi
17exit 0

小技巧

cpulimit 是一个命令行工具,用于限制特定进程的 CPU 使用率。它通过监控和暂停进程的运行来限制其占用的 CPU 时间。以下是它的主要用途:

  1. 控制单个进程的 CPU 使用率:避免某些进程占用过多的 CPU 资源,从而影响系统的整体性能。

  2. 提升系统稳定性:在运行 CPU 密集型任务时,可以限制任务对 CPU 的使用,以便为其他任务保留资源。

  3. 节能和温控:在嵌入式系统或笔记本电脑上,通过限制 CPU 密集型任务的资源占用,可以降低功耗和温度。

  4. 防止进程导致系统卡顿:对某些高负载的进程进行限制,避免它们影响用户的日常使用。

小技巧

/usr/bin/logger 是一个 Linux 系统中用于发送日志消息到系统日志的命令。系统日志通常由 syslog 或 rsyslog 守护进程管理,日志消息会被写入配置中指定的日志文件。

默认情况下,logger 的日志消息会记录到 /var/log/messages 或 /var/log/syslog 文件中,具体位置取决于系统配置。

CentOS/RHEL 系列: 查看 /var/log/messages:

tail -f /var/log/messages

Debian/Ubuntu 系列: 查看 /var/log/syslog:

tail -f /var/log/syslog

如果系统使用 systemd,日志可能被记录到 journald 中。可以使用 journalctl 查看日志:

journalctl -t mytag
$cat /etc/conf.d/logrotate
# Limit cpu usage so monitoring software does not trigger CPU alarms
# This will only be used if package "cpulimit" is installed
CPULIMIT=50