- Create the start and stop scripts of your application.
#!/bin/bash
cd /home/ubuntu/myapp/
java -jar myapp.jar --server.port=8888 &
#!/bin/bash
sudo fuser 8888/tcp -k || true
- Create a file named
myapp
inside /etc/init.d/
#!/bin/bash
case $1 in
start)
/bin/bash /home/ubuntu/scripts/myapp-start.sh
;;
stop)
/bin/bash /home/ubuntu/scripts/myapp-stop.sh
;;
restart)
/bin/bash /home/ubuntu/scripts/myapp-stop.sh
/bin/bash /home/ubuntu/scripts/myapp-start.sh
;;
esac
exit 0
- Mark
myapp
as executable:
cd /etc/init.d/
sudo chmod +x myapp
- Make the script start on boot:
sudo update-rc.d myapp defaults
On Ubuntu 16.04 and above it will give a warning "insserv: warning: script missing LSB tags and override" but I am not sure if the warning can prevent it from running