2

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.

3
  • Are all of the scripts (the init script, start.sh and stop.sh) executable? What happens when you run /etc/init.d/myapp start manually? Commented Jul 23, 2014 at 14:47
  • @AndrewStubbs all files have been given 777... running myapp start manually gives "unable to access jarfile ...." That seems to be the problem, however, the jar is in the same folder as the start.sh Commented Jul 23, 2014 at 14:50
  • @AndrewStubbs ah, there we go... I see now, so even though I run a script that's in the same folder as the jar file, the main path stays the same. Adding cd /var/lib/myapp to the /etc/init.d/myapp file before running the start.sh did the trick. Thanks Commented Jul 23, 2014 at 14:56

2 Answers 2

2

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 &
Sign up to request clarification or add additional context in comments.

5 Comments

That did the trick. Seeing that I need the Java application to access some files with relative paths, changing the directory with cd works best for my solution. Thanks mate
Hm... apparently even though i can manually run the /etc/init.d/myapp start, it still isnt running after rebooting...
you probably need the full path to the java executable, too
@OsaSoft Presumably you did run the update-rc.d myscript defaults step from the thread you linked? If so, I imagine it's what Gus said, change java to wherever your java binary is, which you can find with which java
@Gus That indeed solved the issue. Interesting how in one case it needs the full path to the binary, while in the other not. Anyway thanks a lot to both of you
0

Check if your service is registered properly via chkconfig

$ chkconfig --list

If not you can see your service listed on the output, then try adding this lines to your script

#!/bin/bash
# chkconfig: 2345 95 20 
# description: bla bla
# processname: myapp

and then run

chkconfig --add myapp

For more information you can check the man page for chkconfig

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.