I have a .jar file which I run like this:

java -jar server.jar 4242

So I have a script java-server.sh to launch it:

java -jar server.jar 4242 &

server.jar should start working on system boot, so I tried:

  1. Adding @reboot /home/user/java-server.sh to crontab -e
  2. Adding bash java-server.sh || exit 1 to rc.local
  3. Adding java-server.sh to /etc/init.d

None of which work. If I log into the system and launch my script I works like clockwork until I log out. What is the problem here?

  • 1
    Regarding the last sentence (logging in, running script, logging out and it stops running) it sounds like systemd is at play and is killing any remaining background processes that are running. You'll need to either start the process using a special command (e.g. systemd-run --user -scope script.sh) or modify logind.conf to disable the killing of background processes. linux.slashdot.org/story/16/05/29/212204/… – Gene Aug 16 '16 at 7:47

You should call your script will full path, for example put in /etc/rc.local:

 bash /usr/local/bin/java-server.sh > /tmp/java-server.log 2>&1

if your script is in /usr/local/bin, of course... This will also create a log file to debug other potential problems. (Note that in production you should never create such log files in /tmp for security reasons, but in /var/log with proper permission, rettention and log rotating, but this is simplified for purposes of answer)

| improve this answer | |
  • Thank you for the log idea. It helped me understand that I should actually have put a full path for server.jar in the first place. – Max Aug 16 '16 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.