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.

2
  • You only read one line while your process may display many line. See a working example here: stackoverflow.com/questions/16714127/… Commented Oct 12, 2018 at 12:38
  • @Stephane I mean br.readLine() is null Commented Oct 12, 2018 at 12:40

2 Answers 2

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();
          }
Sign up to request clarification or add additional context in comments.

Comments

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,

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.