0

I want to check the status of a service running on linux. On machine i use the command "systemctl is-active service-name" to check the status of services.And it gives the output as active/inactive(when service is running/not running).

I want to get the status of the service in java. how i do that?

I tried this..

String SERVER_STATUS = new String[]{SUDO_COMMAND, "systemctl is-active vpnd.service"};
try {
    final Process process = new ProcessBuilder(SERVER_STATUS).start();
    process.waitFor();
    InputStream in = process.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String line = null;
    System.out.println("status: " + br.readLine());
} catch (IOException | InterruptedException e) {}

But br.readLine() is coming as null.

ritesh kumar
  • 81
  • 1
  • 7
  • You only read one line while your process may display many line. See a working example here: https://stackoverflow.com/questions/16714127/how-to-redirect-process-builders-output-to-a-string – StephaneM Oct 12 '18 at 12:38
  • @Stephane I mean br.readLine() is null – ritesh kumar Oct 12 '18 at 12:40

2 Answers2

1

Two things I found

  • Please split the command
  • Use absolute path of the command

Example

String SERVER_STATUS[] = new String[]{"sudo", "/usr/bin/systemctl", "is-active",  "java-linux-sample.service"};
        try {
            final Process process = new ProcessBuilder(SERVER_STATUS).redirectErrorStream(true).start();
            process.waitFor();
            InputStream in = process.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            //String line = null;
            System.out.println("status: " + br.readLine());
        } catch (Exception e)
          {
          e.printStackTrace();
          }
0

I assume this is because there is an error running the application. Does your java process have permissions to run systemctl? Is it failing to find the service?

I would advise that you also check the exit value and then check the appropriate error stream.


For example on a Windows machine if I make a mistake in the cmd command

String[] SERVER_STATUS_COMMAND = {"cmd.exe", "/c", "echoo foobar"};

Your program will write

status: null

If we tweak the application slightly we can have the error stream redirected to the output:

final Process process = new ProcessBuilder(SERVER_STATUS_COMMAND)
    .redirectErrorStream(true)
    .start();

then the program would write:

status: 'echoo' is not recognized as an internal or external command,
flakes
  • 21,558
  • 8
  • 41
  • 88
  • I tried with ".redirectErrorStream(true)", Now its writing .. Administrator. It usually boils down to these three things: sudo: no tty present and no askpass program specified, As the service is running as user "nobody", And the command will be executed by user "gateway". So i changed the command as below. SERVER_STATUS = new String[]{"/usr/bin/sudo","systemctl is-active vpnd.service"}; And disabled password as below.. echo 'gateway ALL=(ALL) NOPASSWD: /usr/bin/systemctl is-active vpnd.service' >> "/etc/sudoers" Still facing same issue. – ritesh kumar Oct 13 '18 at 12:37