LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-25-2015, 09:24 AM   #1
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,542

Rep: Reputation: 101Reputation: 101
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.

Last edited by rylan76; 06-25-2015 at 09:33 AM.
 
Old 06-25-2015, 11:03 AM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871
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?
 
Old 06-26-2015, 03:09 AM   #3
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,542

Original Poster
Rep: Reputation: 101Reputation: 101
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
 
1 members found this post helpful.
Old 06-26-2015, 03:30 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871Reputation: 4871
Nice that you solved the problem. Thanks for posting back the solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
boot to java app WakilNow Linux - Newbie 6 01-20-2012 10:38 AM
Not Enough Memory when attempting to start java app ochieman2000 Linux - Newbie 3 10-23-2011 11:13 PM
centos 5: need to start a java prog at boot/startup deostroll Linux - Server 1 12-11-2009 02:40 PM
[RH8] Java app on start & window managers cthu Red Hat 1 11-13-2006 04:25 AM
requiring root password for app access? mysticpain Linux - Software 3 06-08-2004 08:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration