How to make a jar file run on startup & and when you log out?

118,030

Here's a easy way to do that using SysVInit. Instructions:

  1. Create the start and the stop script of your application. Put it on some directory, in our example is:

    • Start Script: /usr/local/bin/myapp-start.sh
    • Stop Script: /usr/local/bin/myapp-stop.sh

    Each one will provide the instructions to run/stop the app. For instance the myapp-start.sh content can be as simple as the following:

    #!/bin/bash
    
    java -jar myapp.jar 
    

    For the stop script it can be something like this:

    #!/bin/bash
    # Grabs and kill a process from the pidlist that has the word myapp
    
    pid=`ps aux | grep myapp | awk '{print $2}'`
    kill -9 $pid
    
  2. Create the following script (myscript) and put it on /etc/init.d.

    /etc/init.d/myscript content:

    #!/bin/bash
    # MyApp
    #
    # description: bla bla
    
    case $1 in
        start)
            /bin/bash /usr/local/bin/myapp-start.sh
        ;;
        stop)
            /bin/bash /usr/local/bin/myapp-stop.sh
        ;;
        restart)
            /bin/bash /usr/local/bin/myapp-stop.sh
            /bin/bash /usr/local/bin/myapp-start.sh
        ;;
    esac
    exit 0
    
  3. Put the script to start with the system (using SysV). Just run the following command (as root):

    update-rc.d myscript defaults 
    

PS: I know that Upstart is great and bla bla, but I preffer the old SysV init system.

Share:
118,030

Related videos on Youtube

Exeleration-G
Author by

Exeleration-G

I'm not a professional, I'm just trying to help other people with the knowledge I gained myself.

Updated on September 18, 2022

Comments

  • Exeleration-G
    Exeleration-G over 1 year

    I have no idea where to start looking. I've been reading about daemons and didn't understand the concept.

    More details :

    • I've been writing a crawler which never stops and crawlers over RSS in the internet.
    • The crawler has been written in java - therefore its a jar right now.
    • I'm an administrator on a machine that has Ubuntu 11.04 .
    • There is some chances for the machine to crash , so I'd like the crawler to run every time you startup the machine.
    • Furthermore, I'd like it to keep running even when i logged out. I'm not sure this is possible, but most of the time I'm logged out, and I still want to it crawl.

    Any ideas? Can someone point me in the right direction?

    Just looking for the simplest solution.

  • Admin
    Admin about 12 years
    And when i logout , will it still run on background?
  • John John Pichler
    John John Pichler about 9 years
    But how do the system will know what parameter will use? How to set to be the "start". Tried here and it did not work.
  • Tobia
    Tobia almost 9 years
    This works, but the command "service myapp start" do not exit the command.
  • Codevalley
    Codevalley over 8 years
    Now how to use the start|stop|restart options in myscript ?
  • Codevalley
    Codevalley over 8 years
    Also, even after doing the 3 steps, my jar is not running on startup. Are you sure if we have to change permissions, or path or something like that?
  • Matt Schuetze
    Matt Schuetze over 8 years
    @Codevalley, as I mentioned, this method is for the SysV init. To use service or other command you'll need to write a upstart job. But if you are going to do this, you should write a systemd one (since it is becoming the standard)
  • er-han
    er-han about 7 years
    This worked for me, thank you. But I had to make the last script ('myscipt' in your example) executable with this command: chmod 755 /etc/init.d/myscript . Otherwise update-rc.d command couldn't execute it.
  • Jared Chu
    Jared Chu almost 7 years
    save my day, I just forgot to chkconfig myscript on
  • Admin
    Admin over 5 years
    From the Upstart website, as of 2019: "Project is in maintaince mode only. No new features are being developed and the general advice would be to move over to another minimal init system or systemd"
  • Vlad
    Vlad over 4 years
    I have update-rc.d: error: myscript Default-Start contains no runlevels, aborting. So it also requires some headings like here