Skip to content

Instantly share code, notes, and snippets.

@pancudaniel7
Created January 17, 2024 21:25
Show Gist options
  • Save pancudaniel7/b7f24bb94f04b31a105ce97ba5c3eaa7 to your computer and use it in GitHub Desktop.
Save pancudaniel7/b7f24bb94f04b31a105ce97ba5c3eaa7 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Logrotate Configuration
echo "Setting up Logrotate configuration for your application..."
cat <<EOF > /etc/logrotate.d/logrotate_size_check
/var/log/syslog {
size 100M
rotate 30
compress
missingok
notifempty
delaycompress
postrotate
/usr/lib/rsyslog/rsyslog-rotate
endscript
}
EOF
cat <<EOF > /etc/logrotate.d/rsyslog
/var/log/syslog
{
rotate 7
daily
missingok
notifempty
delaycompress
compress
postrotate
/usr/lib/rsyslog/rsyslog-rotate
endscript
}
/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
{
rotate 4
weekly
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
/usr/lib/rsyslog/rsyslog-rotate
endscript
}
EOF
echo "Creating Systemd service file for Logrotate..."
cat <<EOF > /etc/systemd/system/logrotate_size_check.service
[Unit]
Description=Rotate log files if size exceeded
[Service]
ExecStart=/usr/sbin/logrotate /etc/logrotate.conf
EOF
# Systemd Timer File
echo "Creating Systemd timer file for Logrotate..."
cat <<EOF > /etc/systemd/system/logrotate_size_check.timer
[Unit]
Description=Run logrotate every 10 minutes
[Timer]
OnCalendar=*:0/10
Persistent=true
[Install]
WantedBy=timers.target
EOF
# Reloading systemd daemon and enabling the timer
echo "Reloading systemd daemon and enabling the timer..."
sudo systemctl daemon-reload
sudo systemctl enable logrotate.timer
sudo systemctl start logrotate.timer
# Check status of the timer
echo "Checking the status of the timer..."
sudo systemctl list-timers --all
echo "Logrotate configuration with systemd timer is set up."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment