Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a jar file that listen to tcpip requests and send data accordingly. this jar file needs to run on a service in raspbian.

i've followed the instructions on this link

I've create sh file and put it in /usr/local/bin (Tourmaline.sh):

#!/bin/sh
SERVICE_NAME=Tourmaline
PATH_TO_JAR=/usr/local/bin/Tourmaline.jar
PID_PATH_NAME=/tmp/Tourmaline-pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
exec java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stopping ...";
kill $PID;
echo "$SERVICE_NAME stopped ...";
rm $PID_PATH_NAME
echo "$SERVICE_NAME starting ..."
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
esac

when i run "sudo Tourmaline.sh start" on the terminal, the program starts to work and when i send a request i'm getting a response.

this was phase 1. next, i've created a service script and put it in /etc/systems/system (Tourmaline.service):

[Unit]
Description = Tourmaline
After= network.target

[Service]
Type = forking
ExecStart = /usr/local/bin/Tourmaline.sh start
ExecStop = /usr/local/bin/Tourmaline.sh stop
ExecReload = /usr/local/bin/Tourmaline.sh reload

[Install]
WantedBy=multi-user.target
I've enable the service using "sudo systemctl enable Tourmaline" and start it with "sudo systemctl start Tourmaline.service"

in the status i can see that the service started and active However, when i send a request i'n getting "connection refused" error i try to run "sudo Tourmaline.sh start" but it says

the program is already running (because the service started).



how can i make the service accepts connections?

What I have tried:

i used netstat -l to see used ports:
when i use Tourmaline.service no tcp is listenning
when i use Tourmaline.sh i'm getting tcp6 0 0 [::]:rtcm-sc104 [::]:* LISTEN
Posted
Updated 8-May-18 1:47am

1 solution

Your Java application is not listening when started by the systemd script.

Such applications should check for errors (here when creating a listening socket) and print an error message and terminate with a non-zero return value upon errors. The calling script should check the return value, clean up, and print a failure message.

But that would not solve the problem itself. So why is the application working when started manually and not when started at boot time as service?

Probably because the network is not up yet!

You have used
[Unit]
Description = Tourmaline
After= network.target
to start your service after starting the network. But that is not sufficient because the network setup requires some time until the interfaces are up.

To ensure that the network is up use
After=network-online.target
Wants=network-online.target
See also NetworkTarget[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900