Skip to content

Instantly share code, notes, and snippets.

@marlonbernardes
Last active October 18, 2021 16:12
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save marlonbernardes/eef26b818270ef3b6d02 to your computer and use it in GitHub Desktop.
Save marlonbernardes/eef26b818270ef3b6d02 to your computer and use it in GitHub Desktop.
How to make a jar file run on startup
  1. Create the start and stop scripts of your application.
  • Example:

myapp-start.sh

#!/bin/bash
cd /home/ubuntu/myapp/
java -jar myapp.jar --server.port=8888 &

myapp-stop.sh

#!/bin/bash
sudo fuser 8888/tcp -k || true
  1. 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
  1. Mark myapp as executable:
cd /etc/init.d/
sudo chmod +x myapp 
  1. Make the script start on boot:
sudo update-rc.d myapp defaults 
@iaubain
Copy link

iaubain commented Oct 1, 2018

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

@mtsoleimani
Copy link

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

Hi,

check this
https://gist.github.com/mtsoleimani/eaf0191f7d7200f04ef9526b63044aeb

@BallerIndustries
Copy link

Looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment