systemd Service:
- java appliaction:
- java install
sudo apt-get install openjdk-17-jdk -y - verify version:
java -version - maven
sudo apt-get install maven -y mvn --version
- java install
spring petclinic build
git clone https://github.com/spring-projects/spring-petclinic.git
cd spring-petclinic
sudo mvn clean package -Dmaven.test.skip=true
Now check target folder:
mkdir /home/ubuntu/spring
sudo cp -rf /home/ubuntu/spring-petclinic/target/spring-petclinic-4.0.0-SNAPSHOT.jar /home/ubuntu/spring
sudo chown ubuntu:ubuntu /home/ubuntu/spring/spring-petclinic-4.0.0-SNAPSHOT.jar
java -jar jarfile-path
Why service: run application as background
benfits:
- auto start
- auto start after reboot
- Easy managment
- logging with jornalctl
systemctl cmds:
- sudo systemctl start <service name> – start application
- sudo systemctl status <service name> – status application
- sudo systemctl restart <service name> – manually restart application
- sudo systemctl enable <service name> – enable to run service after reboot
- sudo systemctl daemon-reload – need to any modification service file (reload service files)
[Unit]
Description=Spring PetClinic Application
Documentation=https://github.com/spring-projects/spring-petclinic.git
After=network.target
[Service]
WorkingDirectory=/home/ubuntu/spring
Environment=SPRING_PROFILES_ACTIVE=prod
Environment=JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
ExecStart=/usr/bin/java -jar /home/ubuntu/spring/spring-petclinic-4.0.0-SNAPSHOT.jar
Restart=always
RestartSec=10
User=ubuntu
Group=ubuntu
[Install]
WantedBy=multi-user.target
