logrotate&timer使用与介绍

logrotate

logrotate 程序是一个日志文件管理工具。用于分割日志文件,删除旧的日志文件,并创建新的日志文件,起到“转储”作用。可以节省磁盘空间。

1、配置文件介绍

Linux系统默认安装logrotate工具,它默认的配置文件在

/etc/logrotate.conf
/etc/logrotate.d/

logrotate.conf是主要的配置文件,logrotate.d是一个目录,该目录里的所有文件都会被主动的读入/etc/logrotate.conf中执行。另外,如果/etc/logrotate.d/里面的文件中没有设定一些细节,则会以/etc/logrotate.conf这个文件的设定来作为默认值。Logrotate是基于CRON来运行的,其脚本是/etc/cron.daily/logrotate,日志轮转是系统自动完成的。实际运行时,logrotate会调用配置文件/etc/logrotate.conf。可以在/etc/logrotate.d目录里放置自定义好的配置文件,用来覆盖Logrotate的缺省值。 所以logrotate的执行是依靠crond的定时任务来执行的,或者通过systemd的timer来定时执行。

2、配置举例

/etc/logrotate.conf

# see "man logrotate" for details

# global options do not affect preceding include directives

# 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

# packages drop log rotation information into this directory
include /etc/logrotate.d

# system-specific logs may also be configured here.

3、转储任务举例

root@Linux:/etc/logrotate.d # cat btmp 
# no packages own btmp -- we'll rotate it here
/var/log/btmp {
    missingok
    monthly
    create 0660 root utmp
    rotate 1
}
root@Linux:/etc/logrotate.d # cat system 
/var/log/sys/messages
/var/log/messages
{
        rotate 1
        size 64M
        compress
        missingok
        notifempty
        olddir /var/log/backup/sys
        sharedscripts
        postrotate
                killall -HUP rsyslogd
        endscript
}

olddir directory : 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
在crond中添加logrotate定时运行任务:

root@Linux:/data0/var-bak/log/syslog # crontab -l
#
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:
MAILTO=""
HOME=/
# run-parts
01 * * * * root cronrunparts /etc/cron.hourly
02 4 * * * root cronrunparts /etc/cron.daily
22 4 * * 0 root cronrunparts /etc/cron.weekly
42 4 1 * * root cronrunparts /etc/cron.monthly

*/5 * * * * /etc/ws.d/backup_log.sh

在/etc/cron.daily中添加logrotate定时执行的脚本

root@Linux:/etc/cron.daily # cat 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 $EXITVALUE
root@Linux:/etc/cron.daily # 

timer介绍

对于systemd具有定时任务的功能还是只有耳闻,并没有实际操作过,实际操作了下,发现也不错。systemd 实际是通过定义一个timer单元来创建一个定时任务。它包含了两大内容,首先定义一个service单元,这个service单元定义了我们想定时执行的任务。然后再定义一个timer单元,通过timer单元定义定时规则去执行之前的service单元。

1、创建定时脚本/opt/now_time.sh

#!/bin/bash
echo "$(date)" >>/tmp/time_record.log

2、创建service单元/usr/lib/systemd/system/timerecord.service

[Unit]
Description=now time service

[Service]
ExecStart=/opt/now_time.sh

[Install]
WantedBy=multi-user.target

3、使能services

systemctl enable timerecord.service

4、创建timer单元/usr/lib/systemd/system/timerecord.timer

[Unit]
Description=Run timerecord  every 5s

[Timer]
OnUnitActiveSec=1m # 可以设置为 5m,1h
Unit=timerecord #指定Service服务名,默认是同名的带有.service后缀的单元

[Install]
WantedBy=timers.target

5、使能timer

systemctl enable timerecord.timer

timer配置介绍

[Unit] # 定义元数据
[Timer] #定义定时器
OnActiveSec:定时器生效后,多少时间开始执行任务
OnBootSec:系统启动后,多少时间开始执行任务
OnStartupSec:Systemd 进程启动后,多少时间开始执行任务
OnUnitActiveSec:该单元上次执行后,等多少时间再次执行
OnUnitInactiveSec: 定时器上次关闭后多少时间,再次执行
OnCalendar:基于绝对时间,而不是相对时间执行,用于和 crond 类似的定时任务 ,以实际时间执行。
AccuracySec:如果因为各种原因,任务必须推迟执行,推迟的最大秒数,默认是60秒
Unit:真正要执行的任务,默认是同名的带有.service后缀的单元
Persistent:如果设置了该字段,即使定时器到时没有启动,也会自动执行相应的单元
WakeSystem:如果系统休眠,是否自动唤醒系统

查看定时任务列表

systemctl list-timers --all

你可能感兴趣的:(linux,运维,服务器)