Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9169479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:53:06+00:00 2026-06-17T15:53:06+00:00

I am trying to use ProcessBuilder to run some external commands, like ifstat or

  • 0

I am trying to use ProcessBuilder to run some external commands, like ifstat or vmstat in Linux.

Such kinds of commands support custom sampling intervals. If I add a sample interval to the external command, for example, ifstat 20, then the command will output like this:

coolcfan@coolcfan-PC:~$ ifstat 20
       eth0               wlan0               vmnet1              vmnet8      
 KB/s in  KB/s out   KB/s in  KB/s out   KB/s in  KB/s out   KB/s in  KB/s out

after 20 seconds

   41.29      1.06      0.36      0.00      0.00      0.00      0.00      0.00

after another 20 seconds

   16.67      0.58      0.38      0.00      0.00      0.00      0.00      0.00

However, when I am running the command using my Java code, the first part of the output will be read after 20 seconds, like this:

Start running "ifstat 20"

after 20 seconds

       eth0               wlan0               vmnet1              vmnet8      
 KB/s in  KB/s out   KB/s in  KB/s out   KB/s in  KB/s out   KB/s in  KB/s out
   66.61      1.73      1.29      0.01      0.00      0.00      0.00      0.00

When I use a NIO server to run the command and use SocketChannel to send the output to the client, the problem is even more severe… (my client needs to wait another 20 seconds to get the output from server after server shows the first output which is 20 seconds after the process start)

And I noticed that the length of the delay of outputing is related to the interval I set to the command.

So why doesn’t the ISR read the output in real time?

A simple test code snippet to demo my question:

public static void main(String[] args) {
    ProcessBuilder pb = new ProcessBuilder();

    pb.command("ifstat 20".trim().split(" "));

    Process p = null;

    System.out.println("Start running \"ifstat 20\"");

    try {
        p = pb.start();

        char[] buf = new char[512];

        InputStreamReader isr = new InputStreamReader(p.getInputStream());

        int count = -1;

        while ((count = isr.read(buf, 0, buf.length)) != -1) {
            System.out.print(new String(buf, 0, count));
        }
    }
    catch (IOException ioe) {
    }
}

Update:

As Peter’s comment, this is not a problem related to Java. It is a piping delay.

But I still don’t understand, why vmstat 20 | cat doesn’t have this delay, while ifstat 20 | cat will delay to show the heads?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:53:06+00:00Added an answer on June 17, 2026 at 3:53 pm

    It does.

    It is the output of the process that is delayed. STDIO does buffering, which varies according as stout is or isn’t a terminal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to use Java's ProcessBuilder to launch an application in Linux that
I am trying to run some Java code I found on linux. maudecmd =
I was trying use a set of filter functions to run the appropriate routine,
Trying to use Powershell to script the removal of specific custom errors from an
I am trying to decide as to whether to use ProcessBuilder or Commons exec,
I'm trying to start an external process via Java using the ProcessBuilder class, and
I am trying use javascript regular expressions to do some matching and I found
I'm trying to get Assetic to run within Symfony 2.0.11 in order to use
I am trying use Thread but i have some problem (I am beginner at
I'm trying use epoch time dates in my series data. The array looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.