简体   繁体   English

以编程方式在Java中检查centos'网络'服务状态

[英]Check centos 'network' service status in java programmatically

I'm performing network service restart in java as below. 我在Java中执行network服务重启,如下所示。

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. 但我需要通过检查network service状态来确保重新启动成功,并在完成后返回。 how to achieve this in Java? 如何用Java实现呢?

You can use service network status to display the network status. 您可以使用service 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? 您确定exitValue为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) (某些进程以0退出,但显示错误)

     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. 除了服务之外,您还可以使用ifdown和ifup ifdown eth0 && ifup eth0 ,将eth0替换为网络接口的名称。 Call the processes like you do for service network restart . 像调用service network restart进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM