Search code examples
javabashunixstartupinit.d

Unix script on startup in /etc/init.d not working


I've been trying to get my Java application to run as a daemon in the background after startup. I've followed the instructions given in the top answer here and to no avail.

This is my /etc/init.d/myapp file:

#!/bin/bash
# MyApp
#
# description: bla bla

case $1 in
    start)
        /bin/bash /var/lib/myapp/start.sh
    ;;
    stop)
        /bin/bash /var/lib/myapp/stop.sh
    ;;
    restart)
        /bin/bash /var/lib/myapp/stop.sh
        /bin/bash /var/lib/myapp/start.sh
    ;;
esac
exit 0

as for the /var/lib/myapp/start.sh, it looks like this:

#!/bin/bash
java -jar myapp-1.0.0RC.jar &

and works fine when run from a terminal via ssh.

i also ran the update-rc.d myscript defaults command, and was only given a warning about headers and LSB

After this, once i reboot the server, the app isnt running. Any help is appreciated.

Thanks.


Solution

  • When bash scripts are run, they are not automatically ran from the same directory that contains them.

    You will either need to update your scripts to change directory to that which holds the scripts before starting the jar:

    #!/bin/bash
    cd /var/lib/myapp/
    java -jar myapp-1.0.0RC.jar &
    

    Or, refer to the jar file with a full path:

    #!/bin/bash
    java -jar /var/lib/myapp/myapp-1.0.0RC.jar &