When running a Java application (or any other service) on a linux machine, it's essential to ensure that the application automatically restarts if it crashes and that the log files are managed properly. This guide will show you how to use supervisord
to run your service and set up log rotation to manage your logs efficiently.
Prerequisites
An AWS EC2 instance with access via SSH.
A Java
.jar
file or any other service you want to manage. For this example we will use a jar file by name “stock-news” whose functionality is to provide relevant news given a stock symbol.
Step 1: Install Supervisor
Supervisor is a process control system that allows you to monitor and control processes. It’s perfect for ensuring your Java application stays up and running.
For Amazon Linux 2 or CentOS:
sudo amazon-linux-extras install epel -y
sudo yum install supervisor -y
sudo systemctl start supervisord //if not started already
sudo systemctl status supervisord
For Ubuntu:
sudo apt-get update
sudo apt-get install supervisor -y
sudo systemctl start supervisord //if not started already
sudo systemctl status supervisord
Step 2: Create a Supervisor Configuration File for Your Service
Now, let's create a Supervisor configuration file for your Java application. This will ensure the application is automatically restarted if it crashes and runs as a managed service.
Create the configuration file:
For Amazon Linux:
sudo vim /etc/supervisord.d/stock-news.ini
For Ubuntu:
sudo vim /etc/supervisor/conf.d/stock-news.ini
[program:stock-news]
command=/usr/bin/java -jar /path/to/your/stock-news-0.1.jar
autostart=true
autorestart=true
stderr_logfile=/var/log/stock-news/stock-news.err.log
stdout_logfile=/var/log/stock-news/stock-news.out.log
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=5
stderr_logfile_maxbytes=50MB
stderr_logfile_backups=5
Replace /path/to/your/stock-news-0.1.jar with the actual path to your .jar file
Step 3: Create the Log Directory
Make sure the directory for the logs exists and is writable:
sudo mkdir -p /var/log/stock-news
sudo chown -R ec2-user:ec2-user /var/log/stock-news
sudo chmod -R 755 /var/log/stock-news
Step 4: Reload and Start Supervisor
After the configuration file is in place, you need to reload Supervisor to pick up the changes and start your service.
Reload Supervisor:
sudo supervisorctl reread
sudo supervisorctl update
Start the service:
sudo supervisorctl start stock-news
Check the status of your service:
sudo supervisorctl status stock-news
Step 5: Set Up Log Rotation
Managing logs effectively is critical for preventing your system from running out of disk space. You can set up log rotation to handle this.
Create a log rotation configuration file:
Open a new log rotation configuration file in
/etc/logrotate.d/
:
sudo vim /etc/logrotate.d/stock-news
Add the following log rotation configuration:
/var/log/stock-news/*.log {
daily
rotate 7
compress
missingok
notifempty
delaycompress
dateext
dateformat -%Y-%m-%d-%s
create 0640 ec2-user ec2-user
sharedscripts
postrotate
/usr/bin/supervisorctl restart stock-news > /dev/null 2>&1 || true
endscript
}
This configuration:
Rotates the logs daily (
daily
).Keeps the logs for 7 days (
rotate 7
).Compresses the old log files (
compress
).Adds a date and timestamp to the rotated logs (
dateext
anddateformat
).Restarts the service after log rotation (
postrotate
).
Step 6: Test Log Rotation
To test the log rotation setup, you can run the following commands:
Check the rotation configuration:
sudo logrotate -d /etc/logrotate.d/stock-news
Force log rotation:
sudo logrotate -f /etc/logrotate.d/stock-news
Check the log directory to verify that logs are rotated and compressed:
ls /var/log/stock-news/
Step 7: Verify Logs and Service
Check the logs to ensure they are being written correctly:
tail -f /var/log/stock-news/stock-news.out.log
Monitor the service:
sudo supervisorctl status stock-news
This will show the status of the service and any issues that may arise.
Conclusion
Now, your Java application is set up to run as a managed service under Supervisor, and the logs will rotate daily with the date and time of the rotation appended to the filenames. You also have log retention for 7 days, ensuring that your system disk space remains under control.
By following these steps, you can efficiently manage any long-running service on AWS EC2 and ensure stability and log management.