This guide explains how to configure ElastAlert on Ubuntu so it automatically starts after reboot, stores logs in a dedicated file, and manages log rotation. Perfect for adding to your public GitHub repo π.
Suggested Repository Name:
elastalert-service-unit
- OS: Ubuntu 22.04.4 LTS (Amazon EC2)
- Hostname:
ip-10-0-0-222 - Python: 3.x
- ElastAlert Directory:
/opt/elastalert - Config File:
/opt/elastalert/config.yaml
sudo nano /etc/systemd/system/elastalert.service[Unit]
Description=ElastAlert Service
After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/elastalert
ExecStart=/usr/bin/python3 -m elastalert.elastalert --config /opt/elastalert/config.yaml --verbose
Restart=always
RestartSec=5
User=root
StandardOutput=append:/var/log/elastalert/elastalert.log
StandardError=append:/var/log/elastalert/elastalert.log
[Install]
WantedBy=multi-user.targetsudo systemctl daemon-reload
sudo systemctl enable elastalert
sudo systemctl start elastalertβ Check service status:
sudo systemctl status elastalertπ View logs:
tail -f /var/log/elastalert/elastalert.logsudo mkdir -p /var/log/elastalert
sudo touch /var/log/elastalert/elastalert.log
sudo chown root:root /var/log/elastalert/elastalert.logπ‘ Ensure proper permissions so ElastAlert can write logs.
sudo nano /etc/logrotate.d/elastalert/var/log/elastalert/elastalert.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
copytruncate
}
- π daily β Rotate logs every day
- π rotate 7 β Keep only the last 7 rotations
- π¦ compress β Gzip old logs
- β³ delaycompress β Compress starting from the 2nd rotation
- β missingok β Skip if file is missing
- π« notifempty β Donβt rotate empty files
- βοΈ copytruncate β Truncate active log safely while running
sudo logrotate -f /etc/logrotate.d/elastalert
ls -lh /var/log/elastalert/Expected output:
elastalert.log
elastalert.log.1.gz
elastalert.log.2.gz
...
elastalert.log.7.gz
- β ElastAlert runs automatically on reboot.
- π Logs stored at
/var/log/elastalert/elastalert.log. - β»οΈ Logs rotate daily, with 7-day retention and compression.
Add screenshots of your service status and logrotate output to your repoβs README for better visuals π.
π¬ Contribution: PRs welcome to improve this guide or automate it via a bash script.
π License: MIT