Logrotation: Difference between revisions

From HPCWIKI
Jump to navigation Jump to search
(Created page with "== Linux log rotation == Controlling the sizes of log files on a Linux server is crucial due to their continuous growth. As log files accumulate, they can consume valuable storage space, server resources, and cause performance issues. To address this problem, log rotation is commonly employed in most Linux distributions.<ref>https://betterstack.com/community/guides/logging/how-to-manage-log-files-with-logrotate-on-ubuntu-20-04/</ref><syntaxhighlight lang="bash"> $ logro...")
 
(Fix: remove --- horizontal lines (7 removed))
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Linux log rotation ==
{{Status
|status=Draft
|owner=Knowledge Agent
|last_update=2026-07-16
|review=Pending
}}
 
{{TOC}}
 
== Overview ==
 
Logrotation에 대한 기술 문서입니다.
 
=== Summary ===
 
* 무엇인가? - Logrotation
* 왜 필요한가? - HPC 및 서버 환경에서 필수 개념
* 언제 사용하는가? - 서버 구성, 성능 튜닝, 문제 해결 시
 
 
== Purpose ==
 
이 문서가 존재하는 이유
 
* Goal: Logrotation에 대한 기술 정보 제공
* Scope: Logrotation의 개념, 사용법, 설정
* Non-goals: 다른 주제로의 확장
 
 
== Key Concepts ==
 
{| class="wikitable"
! Concept
! Description
! Related
|-
| Logrotation
| HPC/서버 환경에서 중요한 기술 개념
| [[Linux]], [[Server]]
|}
 
 
== Detailed Explanation ==
 
Controlling the sizes of log files on a [[Linux]] server is crucial due to their continuous growth. As log files accumulate, they can consume valuable storage space,  server resources, and cause performance issues. To address this problem, log rotation is commonly employed in most Linux distributions.<ref>https://betterstack.com/community/guides/logging/how-to-manage-log-files-with-logrotate-on-ubuntu-20-04/</ref><syntaxhighlight lang="bash">
Controlling the sizes of log files on a [[Linux]] server is crucial due to their continuous growth. As log files accumulate, they can consume valuable storage space,  server resources, and cause performance issues. To address this problem, log rotation is commonly employed in most Linux distributions.<ref>https://betterstack.com/community/guides/logging/how-to-manage-log-files-with-logrotate-on-ubuntu-20-04/</ref><syntaxhighlight lang="bash">
$ logrotate --version
$ logrotate --version
logrotate 3.19.0
logrotate 3.19.0
     Default mail command:      /usr/bin/mail
     Default mail command:      /usr/bin/mail
     Default compress command:  /bin/gzip
     Default compress command:  /bin/gzip
Line 12: Line 54:
     SELinux support:            yes
     SELinux support:            yes
</syntaxhighlight>
</syntaxhighlight>
== Main configuration ==
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
The main Logrotate configuration
The main Logrotate configuration
$cat /etc/logrotate.conf
$cat /etc/logrotate.conf
# see "man logrotate" for details
# see "man logrotate" for details
# global options do not affect preceding include directives
# global options do not affect preceding include directives
# rotate log files weekly, the frequency of log rotation. Alternatively, you can specify another time interval (hourly, daily, monthly, or yearly).
# rotate log files weekly, the frequency of log rotation. Alternatively, you can specify another time interval (hourly, daily, monthly, or yearly).
weekly
weekly
# use the adm group by default, since this is the owning group
# use the adm group by default, since this is the owning group
# of /var/log/syslog.
# of /var/log/syslog.
su root adm


# keep 4 weeks worth of backlogs
# log files are rotated four times before old ones are removed.
# If rotate is set to zero, then the old versions are removed immediately and not rotated.
# If it is set to -1, the older logs will not be remove at all except if affected by maxage.
rotate 4


# create new (empty) log files after rotating old ones
== Best Practices ==
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
== References ==
include /etc/logrotate.d
 
# system-specific logs may also be configured here.
</syntaxhighlight>
 
== Additional configuration ==
<syntaxhighlight lang="bash">
$ ls -al ls /etc/logrotate.d/
...
-rw-r--r--  1 root root  120 Sep  6  2019 alternatives
-rw-r--r--  1 root root  126 Dec  5  2019 apport
-rw-r--r--  1 root root  173 Apr  9  2020 apt
-rw-r--r--  1 root root    91 Apr  1  2020 bootlog
-rw-r--r--  1 root root  130 Jan 21  2019 btmp
-rw-r--r--  1 root root  209 Sep 19  2021 ufw
-rw-r--r--  1 root root  145 Feb 19  2018 wtmp
...
</syntaxhighlight>
 
== System dependent custom configuration ==
<syntaxhighlight lang="bash">
Create system dependent log rotation config file mylog under /etc/logrotate.d
 
$cat /etc/logrotate.d/mylog
/var/log/mylog/*.log
{
    daily
    missingok
    rotate 7
    compress
    notifempty
}
 
test the new configuration
$sudo logrotate /etc/logrotate.conf --debug --verbose
 
test that the log rotation works without waiting for the specified schedule
$sudo logrotate -f /etc/logrotate.d/mylog
$ls /var/log/mylog/
 
 
verify if a particular log file is rotating or not
$sudo cat /var/lib/logrotate/status | grep mylog


* [https://wiki.hpcmate.com Logrotation]




</syntaxhighlight>
== Related Pages ==


== Modifying  access permissions ==
* [[Linux]]
<syntaxhighlight lang="bash">
* [[Server]]
with create directive, we can change newly created log files will be owned by the <user> <group>
* [[Hardware]]
* [[Network]]


/etc/logrotate.d/mylog


/var/log/mylog.log
[[Category:Server]]
{
== Knowledge Graph ==
    ...
    create 644 <user> <group>
    ...
}


Related


</syntaxhighlight>
→ [[Linux]]
→ [[Server]]
→ [[Hardware]]
→ [[Network]]


== System independent custom configuration ==
[[Category:Reference]]
<syntaxhighlight lang="bash">
create your configuration file outside of /etc/logrotate.d/
 
$cat ~/mylog/logrotate.conf
/home/<user>/mylog/*.log
{
    hourly
    missingok
    rotate 7
    compress
    notifempty
}
 
create a custom Logrotate state file
$logrotate ~/mylog/logrotate.conf --state ~/mylog/logrotate.state
 
create cron jobs by adding At the bottom of the file
$crontab -e
 
0 * * * * /usr/sbin/logrotate /home/<user>/logify/logrotate.conf --state /home/<user>/logify/logrotate.state
 
</syntaxhighlight>
 
== Running commands or scripts before or after log rotation ==
<syntaxhighlight lang="bash">
prerotate directives - executes commands or scripts before log rotation
postrotate directives - executes commands or scripts after log rotation.
Both directives are closed using the endscript directive
 
/home/<user>/mylog/*.log
{
    hourly
    missingok
    rotate 7
    compress
    notifempty
    sharedscripts
    prerotate
      do-pre-something
    endscript
    postrotate
      do-after-something
    endscript
 
}
</syntaxhighlight>
 
== Changing the system Logrotate schedule ==
<syntaxhighlight lang="bash">
Ubuntu logrotate schedule can be found at following path
 
$ ls -ald /etc/cron.*
drwxr-xr-x 2 root root 4096 Feb 25 13:20 /etc/cron.d
drwxr-xr-x 2 root root 4096 Feb 25 13:23 /etc/cron.daily
drwxr-xr-x 2 root root 4096 Feb 25 13:20 /etc/cron.hourly
drwxr-xr-x 2 root root 4096 Feb 25 13:20 /etc/cron.monthly
drwxr-xr-x 2 root root 4096 Feb 25 13:21 /etc/cron.weekly
 
regardless of schedule in above configuration file, we can move configuration file from one to the other cron.xxx path
$ sudo mv /etc/cron.daily/logrotate /etc/cron.hourly
 
 
</syntaxhighlight>
 
== References ==
<references />

Latest revision as of 11:29, 17 July 2026

Template:Status

Template:TOC

Overview

Logrotation에 대한 기술 문서입니다.

Summary

  • 무엇인가? - Logrotation
  • 왜 필요한가? - HPC 및 서버 환경에서 필수 개념
  • 언제 사용하는가? - 서버 구성, 성능 튜닝, 문제 해결 시


Purpose

이 문서가 존재하는 이유

  • Goal: Logrotation에 대한 기술 정보 제공
  • Scope: Logrotation의 개념, 사용법, 설정
  • Non-goals: 다른 주제로의 확장


Key Concepts

Concept Description Related
Logrotation HPC/서버 환경에서 중요한 기술 개념 Linux, Server


Detailed Explanation

Controlling the sizes of log files on a Linux server is crucial due to their continuous growth. As log files accumulate, they can consume valuable storage space, server resources, and cause performance issues. To address this problem, log rotation is commonly employed in most Linux distributions.[1]

$ logrotate --version
logrotate 3.19.0
    Default mail command:       /usr/bin/mail
    Default compress command:   /bin/gzip
    Default uncompress command: /bin/gunzip
    Default compress extension: .gz
    Default state file path:    /var/lib/logrotate/status
    ACL support:                yes
    SELinux support:            yes

<syntaxhighlight lang="bash"> The main Logrotate configuration $cat /etc/logrotate.conf

  1. see "man logrotate" for details
  2. global options do not affect preceding include directives
  3. rotate log files weekly, the frequency of log rotation. Alternatively, you can specify another time interval (hourly, daily, monthly, or yearly).

weekly

  1. use the adm group by default, since this is the owning group
  2. of /var/log/syslog.


Best Practices

  • 최신 버전 사용 권장
  • 공식 문서 참고
  • 테스트 환경에서 먼저 검증


References


Related Pages

Knowledge Graph

Related

LinuxServerHardwareNetwork