How to Automatically Start a Spring Boot Application After an Ubuntu Server Reboot

How to Automatically Start a Spring Boot Application After an Ubuntu Server Reboot

Ensuring that your Spring Boot application starts automatically after a reboot on an Ubuntu server is essential for maintaining uptime and minimizing manual intervention. This can be achieved by using systemd to create a service that manages your application. Below is a step-by-step guide to setting this up.

1. Create a Systemd Service File

First, you'll need to create a service file that defines how your Spring Boot application should be managed by systemd.

Create the Service File

Use a text editor to create a new service file in /etc/systemd/system/. For example:

sudo nano /etc/systemd/system/voice_log_server.service

Add Service Configuration

Insert the following configuration into the service file:

[Unit]
Description=Voice Log Server Application
After=network.target

[Service]
User=root
WorkingDirectory=/root/fifo-ftp-backend/target
ExecStart=/usr/bin/java -jar /root/fifo-ftp-backend/target/CallRecordsViewFTP-0.0.1-SNAPSHOT.jar
Restart=always
RestartSec=10
StandardOutput=append:/var/log/myapp.log
StandardError=append:/var/log/myapp_error.log

[Install]
WantedBy=multi-user.target
  • User: Replace root with the user under which the application should run.
  • WorkingDirectory: Set this to the directory where your JAR file is located.
  • ExecStart: This is the command that will start your Spring Boot application.
  • Restart: This ensures that the application restarts automatically if it fails.
  • RestartSec: Sets the delay (in seconds) before restarting the service.
  • StandardOutput and StandardError: Redirects the application's logs to specific files for easier monitoring.

Save and Close the File

After adding the configuration, save the file and exit the editor. For Nano, you can do this by pressing Ctrl+X, then Y, and finally Enter.

2. Enable and Start the Service

Now that the service file is created, you'll need to reload systemd to recognize the new service, start the service, and enable it to start automatically on boot.

Reload Systemd

To apply the changes, reload the systemd daemon:

sudo systemctl daemon-reload

Start the Service

Start your service with the following command:

sudo systemctl start voice_log_server.service

Enable the Service

To ensure that the service starts automatically on boot, enable it with:

sudo systemctl enable voice_log_server.service

Check the Status

Verify that the service is running:

sudo systemctl status voice_log_server.service

3. Managing the Service

Once the service is up and running, you may need to manage it from time to time. Here are some useful commands:

  • Stop the Service:
    sudo systemctl stop voice_log_server.service
    
  • Restart the Service:
    sudo systemctl restart voice_log_server.service
    
  • View Logs:

    Check the logs you specified in the service configuration:

    tail -f /var/log/myapp.log
    tail -f /var/log/myapp_error.log
    

4. Test the Service

Finally, it's crucial to test that everything is set up correctly.

Reboot Your Server

Reboot your server to ensure that the service starts automatically:

sudo reboot

Verify the Service

After the server restarts, check the status of your service:

sudo systemctl status voice_log_server.service

If everything is configured correctly, your Spring Boot application should start automatically after the reboot, ensuring that it remains up and running with minimal manual intervention.

By following these steps, you can confidently set up your Spring Boot application to handle server reboots seamlessly, keeping your services available and reducing downtime.

Comments