Deploy spring boot jar as a system service on linux

intruder
2 min readJul 16, 2023

--

If you prefer to use the systemctl service option to deploy and manage your Spring application on an Amazon EC2 instance, you can follow these steps:

  1. Connect to the EC2 instance: Connect to your EC2 instance using SSH.
  2. Install Java: Ensure that Java is installed on the EC2 instance. If it is not already installed, you can use the appropriate package manager to install it. For example, on Ubuntu-based systems, you can run sudo apt update && sudo apt install default-jre.
  3. Create a Systemd service file: Create a Systemd service file to define how your Spring application should be managed. Create a new file in the /etc/systemd/system/ directory with a .service extension, such as /etc/systemd/system/my-spring-app.service.
  4. Open the file with a text editor and add the following content:
[Unit]
Description=My Spring Application
After=syslog.target

[Service]
User=ec2-user
ExecStart=/usr/bin/java -jar /path/to/your/file.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

Make sure to replace /path/to/your/file.jar with the actual path to your Spring JAR file, and update the User field with the appropriate user.

Enable and start the service: Once the service file is created, enable and start the service using the systemctl commands.

sudo systemctl enable my-spring-app 
sudo systemctl start my-spring-app
  1. This will enable the service to start automatically at boot and start the application.
  2. Check the status and logs: You can check the status of your Spring application service using the following command:
sudo systemctl status my-spring-app

Additionally, you can view the logs of your application using the journalctl command:

sudo journalctl -u my-spring-app

This will provide information about the current status of the service and any logged output from your application.

With these steps, your Spring application will be deployed as a systemd service on your EC2 instance. It will automatically start on boot and continue running even after you close the terminal. You can manage the application using systemctl commands, such as start, stop, restart, and status.

--

--