How to Run a Spring Boot Application JAR File in the Background on Ubuntu

Spring Boot applications are incredibly popular for building robust and scalable applications. Once your Spring Boot project is packaged into a .jar file, the next step is to deploy and run it on a server. If you’re using Ubuntu, you may want to ensure that your application runs smoothly in the background, even after you log out of the terminal or reboot your server.

In this blog, we’ll explore four simple and effective ways to run your Spring Boot JAR file in the background. Let’s dive in!


Why Run a JAR File in the Background?

Running your application in the background ensures that:

  1. It continues to operate even after you close the terminal.
  2. It remains accessible to users without manual intervention.
  3. It can be restarted automatically after a failure or server reboot.

Prerequisites

  1. A Spring Boot JAR file (e.g., myapp.jar).
  2. Java installed on your Ubuntu machine (e.g., OpenJDK 17+).
  3. Access to the server via terminal (SSH or local).

Method 1: Using nohup

The nohup command allows you to run processes in the background and keeps them running even if you log out.

Steps to Run Using nohup:

  1. Navigate to the directory containing your JAR file:bashCopy codecd /path/to/your/jar
  2. Run the JAR file in the background:bashCopy codenohup java -jar myapp.jar > output.log 2>&1 &
    • nohup: Ensures the process doesn’t stop after logout.
    • > output.log: Logs the application’s output.
    • 2>&1: Redirects errors to the same log file.
    • &: Runs the command in the background.
  3. Check the running process:bashCopy codeps aux | grep myapp.jar
  4. Stop the application if needed:bashCopy codekill <process-id>

Pros:

  • Quick and easy to use.
  • Ideal for testing or temporary setups.

Cons:

  • Less control and monitoring capabilities.

Method 2: Using screen

screen is a terminal multiplexer that lets you create a virtual terminal session. You can detach and reattach to the session later.

Steps to Run Using screen:

  1. Install screen:bashCopy codesudo apt install screen -y
  2. Start a new screen session:bashCopy codescreen -S myapp
  3. Run your JAR file:bashCopy codejava -jar myapp.jar
  4. Detach from the session (keep the app running): Press Ctrl+A, then D.
  5. Reattach to the session:bashCopy codescreen -r myapp
  6. List active sessions:bashCopy codescreen -ls

Pros:

  • Allows interaction with the running application.
  • Easy to monitor and reattach if needed.

Cons:

  • Requires manual detachment.

Method 3: Using tmux

Similar to screen, tmux is another terminal multiplexer.

Steps to Run Using tmux:

  1. Install tmux:bashCopy codesudo apt install tmux -y
  2. Start a new tmux session:bashCopy codetmux new -s myapp
  3. Run your application:bashCopy codejava -jar myapp.jar
  4. Detach from the session: Press Ctrl+B, then D.
  5. Reattach to the session:bashCopy codetmux attach -t myapp

Pros:

  • Lightweight and easy to use.
  • Allows interaction with the process.

Cons:

  • Requires manual detachment.

Method 4: Using systemd (Recommended for Production)

systemd is a system and service manager used in Linux distributions. It is ideal for production environments as it ensures the application starts on boot and restarts automatically if it fails.

Steps to Run Using systemd:

  1. Create a systemd service file:bashCopy codesudo nano /etc/systemd/system/myapp.service
  2. Add the following content to the file:iniCopy code[Unit] Description=Spring Boot Application After=network.target [Service] User=your-username ExecStart=/usr/bin/java -jar /path/to/myapp.jar Restart=always RestartSec=10 StandardOutput=file:/var/log/myapp/output.log StandardError=file:/var/log/myapp/error.log [Install] WantedBy=multi-user.target
  3. Reload the systemd daemon:bashCopy codesudo systemctl daemon-reload
  4. Start the service:bashCopy codesudo systemctl start myapp
  5. Enable the service to start on boot:bashCopy codesudo systemctl enable myapp
  6. Check the status of the service:bashCopy codesudo systemctl status myapp
  7. View logs:bashCopy codesudo journalctl -u myapp -f

Pros:

  • Automatically starts on server boot.
  • Reliable for production use.
  • Monitors the application and restarts it if it crashes.

Cons:

  • Requires additional setup.

Conclusion

Running your Spring Boot application in the background is essential for smooth operation on servers. While methods like nohup, screen, and tmux are quick and effective, using systemd is highly recommended for production environments. It offers robust monitoring, automatic restarts, and integration with the Linux system.

Choose the method that best suits your needs and start hosting your Spring Boot application with ease!

Leave a Reply

Your email address will not be published. Required fields are marked *