-
Notifications
You must be signed in to change notification settings - Fork 44
6. Running the Application as a Service
Running an application as a service is crucial for ensuring its reliability. When the server restarts, the application will automatically start without manual intervention. Additionally, the application can operate independently of user sessions, ensuring continuous operation even if no users are logged into the system.
To run the Java application as a service, follow the below steps:
- Navigate to the system directory on your server (Development Environment)
/etc/systemd/system/
The directory /etc/systemd/system/
is used to store system-specific unit files for systemd, the system and service manager for Linux operating systems.
- Create a file for the Java application
nano java_app.service
- Paste the below configuration in the file
[Unit]
Description=Java Spring Boot Application
After=network.target
[Service]
User=dhee
WorkingDirectory=/home/user/hng_boilerplate_java_web
ExecStart=/home/user/hng_boilerplate_java_web/mvnw spring-boot:run
StandardOutput=file:/home/user/hng_boilerplate_java_web/app.log
StandardError=file:/home/user/hng_boilerplate_java_web/app.log
#Restart=on-failure
[Install]
WantedBy=multi-user.target
This above unit file configures systemd
to manage the Java Spring Boot application, ensuring it starts after the network is up, runs under a specific user, operates from a designated directory, logs output to a file, and can be enabled to start automatically at boot time in multi-user mode.
Note: Replace user
with the name of the user of the dev environment
- Reload the
systemd
manager configuration
sudo systemctl daemon-reload
- Start the java_app.service file as a
systemd
service
sudo systemctl start java_app.service
Repeat the configuration for staging and prod environments with the appropriate usernames.
Made by Dhee ‖ Sudobro ‖ Stephennwachukwu ‖ Dominic-source