Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running jar as a Linux service - init.d script gets stuck starting app

I am currently working on implementing a runnable jar as a background service on a Linux VM box. I have used the example found here as a base to work on, and have modified the start() method into this:

start() {

# Start application

java -jar /home/vagrant/sagepay-stub-1.4.jar >/var/log/sagepay-stub.log 2>&1

PID=$!

echo $PID > pid.txt

}

This sets up the service to write output to the log sagepay-stub.log and saves the PID for use when the service stop method is called.

Here is the handler for the start command:

case "$1" in start)

echo "Starting $APP"
start
echo "$APP started."
;;

When I call the method i get the "Starting Sagepay-stub" output, but then I am stuck inside the script. All i can do is Ctrl & C to quit, which gives me output:

"Sagepay-stub started."

Now i look in the logs and see that the output is as expected - the stub server has started successfully. But i cannot wget on the port (i have opened the relevant ports using iptables) - the connection is refused :

Connecting to localhost|127.0.0.1|:8889... failed: Connection refused.

Any ideas of what the problem is are appreciated. I think the problem lies with starting the app then moving on and letting it run in the background. The script get stuck waiting for something while the app starts. The jar runs fine locally without input.

If the problem lies with the PID commands (which I found on another thread as an accepted answer) how can I comment these out and still be able to stop the service?

Comments on code also welcome

thanks

like image 420
a.hrdie Avatar asked Oct 08 '13 12:10

a.hrdie


1 Answers

Haven't you forgotten a '&' after the line 'java -jar [application stuff]' so that execution continues after that line.

It's exactly the same as if you run the command in console. A '&' after ensures that the console is usable after application start.

That is why it prints "Sagepay-stub started." once you hit ctrl-c because that is when you kill the java process and the bash script continues.

like image 198
LiquidityC Avatar answered Sep 23 '22 05:09

LiquidityC