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:
- It continues to operate even after you close the terminal.
- It remains accessible to users without manual intervention.
- It can be restarted automatically after a failure or server reboot.
Prerequisites
- A Spring Boot JAR file (e.g.,
myapp.jar
). - Java installed on your Ubuntu machine (e.g., OpenJDK 17+).
- 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
:
- Navigate to the directory containing your JAR file:bashCopy code
cd /path/to/your/jar
- Run the JAR file in the background:bashCopy code
nohup 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.
- Check the running process:bashCopy code
ps aux | grep myapp.jar
- Stop the application if needed:bashCopy code
kill <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
:
- Install
screen
:bashCopy codesudo apt install screen -y
- Start a new
screen
session:bashCopy codescreen -S myapp
- Run your JAR file:bashCopy code
java -jar myapp.jar
- Detach from the session (keep the app running): Press
Ctrl+A
, thenD
. - Reattach to the session:bashCopy code
screen -r myapp
- List active sessions:bashCopy code
screen -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
:
- Install
tmux
:bashCopy codesudo apt install tmux -y
- Start a new
tmux
session:bashCopy codetmux new -s myapp
- Run your application:bashCopy code
java -jar myapp.jar
- Detach from the session: Press
Ctrl+B
, thenD
. - Reattach to the session:bashCopy code
tmux 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
:
- Create a
systemd
service file:bashCopy codesudo nano /etc/systemd/system/myapp.service
- 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
- Reload the
systemd
daemon:bashCopy codesudo systemctl daemon-reload
- Start the service:bashCopy code
sudo systemctl start myapp
- Enable the service to start on boot:bashCopy code
sudo systemctl enable myapp
- Check the status of the service:bashCopy code
sudo systemctl status myapp
- View logs:bashCopy code
sudo 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!