0

I'm performing network service restart in java as below.

String[] serviceStop = { "service","network","restart" };
Process networkProc = new ProcessBuilder(serviceStop).start();
networkProc.waitFor();
Integer returncode=networkProc.exitValue();

which returns success code even if it is still restarting. but I need to make sure restart went successfully and return once complete by checking the network service status. how to achieve this in Java?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Manjunath
  • 111
  • 1
  • 8

1 Answers1

0

You can use service network status to display the network status. With loop and sleep you should achieve what you want but I'm realy surprised about your need.

  • Are you sure the exitValue is 0?
  • Are you sure that you have all permissions to execute this command ?
  • Do you look at the process output ? (some processes exit with 0 but display an error)

    Process networkProc = new ProcessBuilder(serviceStop)
                    .redirectOutput(new File("serviceRestartOuput.txt"))
                    .start();
    

Instead of service, you can also use ifdown and ifup ifdown eth0 && ifup eth0 , replace eth0 with the name of your network interface. Call the processes like you do for service network restart.

N. Brun
  • 729
  • 5
  • 5
  • my issue is im changing Static IP address of VM. and calling service network restart. sometimes it gives already in use. with success code. how to make sure it restarted success fully? – Manjunath Feb 25 '16 at 09:03
  • If it restarted successfully then you can use the network. So you can use service network status, ping or InetAddress.isReachable to know if network OK. For more information see this [question](http://stackoverflow.com/questions/1402005/how-to-check-if-internet-connection-is-present-in-java#1402020). – N. Brun Feb 25 '16 at 17:52