[Java] deploying jar to Ubuntu as a service

Time:2021-3-5

1. Install JDK

apt install openjdk-11-jdk

2. Create a service profile

vim /ect/systemd/system/my_service.service

The following contents are added

[Unit]
Description=My Java Service
[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/server
ExecStart=/usr/bin/java -jar /home/ubuntu/server/main.jar
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target

3. Update, run and start service configuration

systemctl daemon-reload
systemctl enable my_service.service
systemctl start my_service.service