LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Centos 7 - how to start Java app requiring networking on boot with systemd (https://www.linuxquestions.org/questions/linux-server-73/centos-7-how-to-start-java-app-requiring-networking-on-boot-with-systemd-4175546416/)

rylan76 06-25-2015 09:24 AM

Centos 7 - how to start Java app requiring networking on boot with systemd
 
Hi Guys

I have a Java App that requires networking to be up. In the terminal, I start the app this way with the startverdi.sh script: (it runs in a screen):

Code:

cd /usr/src/verdi
/usr/bin/screen -S verdi -d -m /usr/java/jdk1.8.0_25/bin/java -jar /usr/src/verdi/verdi-1.0-SNAPSHOT-jar-with-dependencies.jar

This works fine from the command line.

I now what to do exactly the above, but when the system starts up.

I have this systemd script - verdi.service - in /etc/systemd/system:

Code:


[Unit]
Description=VerDi
After=network-online.target

[Service]
Type=forking
User=root
Group=root
ExecStart=/usr/bin/screen -S verdi -d -m /usr/java/jdk1.8.0_25/bin/java -jar /usr/src/verdi/verdi-1.0-SNAPSHOT-jar-with-dependencies.jar
TimeoutSec=300

[Install]
WantedBy=multi-user.target

This works fine and starts the app after a

Code:

systemctl enable verdi.service
and a reboot, but then it has no networking - the app is in a state where the JVM clearly cannot access the network.

For example, I can access the app on 127.0.0.1 once I start it manually in a terminal, but if I reboot the system to have it start via systemd, it DOES start, but has no networking.

The same is true if I do

Code:

systemctl start verdi.service
instead of rebooting.

systemctl status verdi.service returns, after a reboot:

Code:

verdi.service - VerDi
  Loaded: loaded (/etc/systemd/system/verdi.service; enabled)
  Active: active (running) since Thu 2015-06-25 16:19:17 SAST; 22s ago
  Process: 9949 ExecStart=/usr/bin/screen -S verdi -d -m /usr/java/jdk1.8.0_25/bin/java -jar /usr/src/verdi/verdi-1.0-SNAPSHOT-jar-with-dependencies.jar (code=exited, status=0/SUCCESS)
 Main PID: 9950 (screen)
  CGroup: /system.slice/verdi.service
          9950 /usr/bin/SCREEN -S verdi -d -m /usr/java/jdk1.8.0_25/bin/java -jar /usr/src/verdi/verdi-1.0-SNAPSHOT-jar-with-dependencies.jar
          9951 /usr/java/jdk1.8.0_25/bin/java -jar /usr/src/verdi/verdi-1.0-SNAPSHOT-jar-with-dependencies.jar

Jun 25 16:19:17 asterisk systemd[1]: Started VerDi.

e. g. systemd considers all things golden, but the Java app itself cannot see the network.

How can I fix this and give my Java App network access when starting it via systemd, just as if I started it from a commandline in the terminal?

Note I have

Code:

After=network-online.target
specified in my service file to systemd, but it appears to have no effect...

Thanks

Stefan

EDIT: I also disabled firewalld in case it was somehow interfering if the Java app was started via systemd, to no effect. The Java app is still not contactible the moment systemd kicks it off for me on system start.

TobiSGD 06-25-2015 11:03 AM

From the systemd website:
Quote:

network-online.target is a target that actively waits until the nework is "up", where the definition of "up" is defined by the network management software.
So, the question is, how do you manage your network? Network-manager? systemd-networkd?

rylan76 06-26-2015 03:09 AM

Hi

Thanks for the reply!

I use networkmanager - however, that was not the problem.

I've managed to solve this in the meantime.

It turns out I needed to change the verdi.service file I passed to systemd. I had to add a line

Code:

WorkingDirectory=/usr/src/verdi
before the

ExecStart

line.

E. g. my verdi.service file now looks like this:

Code:


[Unit]
Description=VerDi
After=network-online.target

[Service]
Type=forking
User=root
Group=root
WorkingDirectory=/usr/src/verdi
ExecStart=/usr/bin/screen -S verdi -d -m /usr/java/jdk1.8.0_25/bin/java -jar /usr/src/verdi/verdi-1.0-SNAPSHOT-jar-with-dependencies.jar
TimeoutSec=300

[Install]
WantedBy=multi-user.target

The Java app now starts up and works correctly with network connectivity.

The reason it was NOT working and it had no network connectivity was that SystemD (with me not specificing WorkingDirectory) started the app in the /etc/systemd/system folder, NOT the /usr/src/verdi where it should have started. It was therefore missing certain configuration files which obviously are not in /etc/systemd/system, but in /usr/src/verdi - among these config files is the one that tells the VerDi app which IP to use... and -that- was why it was not working - it was started by systemd in the wrong directory.

So the verdi.service file was 99% correct, it just needed me to add the WorkingDirectory entry to get it to start the Java app in the correct location.

Regards

Stefan

TobiSGD 06-26-2015 03:30 AM

Nice that you solved the problem. Thanks for posting back the solution.


All times are GMT -5. The time now is 10:42 AM.