Search code examples
javalinuxjarraspberry-piraspbian

How to run a .jar file after every boot on Debian (Raspbian)


I want to run a .jar file always after I booted up my Raspberry Pi. I know how to run the file in the console:

java -jar pi/test.jar

But how can I save this command in a executable file and where is the place to save it, that the file starts after boot up? This is not a duplicate because it's different on a Raspberry Pi than on other linux systems.


Solution

  • you can use my setup :

    save this in /etc/init.d/raspberryUtils (change as appropriate)

    #!/bin/bash
    # MyApp
    #
    # description:raspberryUtils util service
    
    case $1 in
        start)
            /bin/bash /home/developer/raspberryUtils/bootstartup/startServer.sh
        ;;
        stop)
            /home/developer/raspberryUtils/bootstartup/stopServer.sh
        ;;
        restart)
            /home/developer/raspberryUtils/bootstartup/stopServer.sh
            /home/developer/raspberryUtils/bootstartup/startServer.sh
        ;;
    esac
    exit 0
    

    startServer.sh:

    #!/bin/bash
    
    java -cp /home/developer/raspberryUtils/dist/RaspberryUtils.jar service.StartServices  &
    

    stopServer.sh

    #!/bin/bash
    # Grabs and kill a process from the pidlist that has the word myapp
    
    pid=`ps aux | grep RaspberryUtils | awk '{print $2}'`
    kill -9 $pid
    

    this way you can stop,start without restarting the pi too

    NOTE:

    as mentioned by @Cosu its better using jps so stopServer.sh is :

    #!/bin/bash
    # Grabs and kill a process from the pidlist that has the word StartService
    
    pid=`jps | grep StartService | awk '{print $1}'`
    kill -9 $pid