Log rotation in Linux
posted 2022.03.19 by Clark Wilkins

Directly related to the prior post, I needed to start log rolling on an Amazon EC2 server (Amazon Linux 2). In this case, I have multiple logs in a subdirectory of "home" that begin with the project name and end in log (i.e. /home/ec2-user/logs/my-project-api-errors.log).

This configuration file, saved in /etc/logrotate.d does the trick.

/home/ec2-user/logs/my-project*log {
  missingok,
  notifempty,
  size 50k
  rotate 1
  create 0644 root root
  su root root
}

Breaking it down line-by-line:

  1. Include all log files starting with my-project and ending in log.
  2. If nothing is there, don't worry about it.
  3. Don't rotate if the file is empty.
  4. Rotate at 50KB file size.
  5. Keep 1 archived file (per match).
  6. Create the file with root as the owner.
  7. Run this rotation as root.

Line 7 is important because this directory is readable by users other than root, and logrotate will complain if lines 6 and 7 do not match the suer and group.

Credit to this article for the tip.