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 250921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:33:05+00:00 2026-05-11T21:33:05+00:00

Is there a way to periodically run a Unix command ( ps in my

  • 0

Is there a way to periodically run a Unix command (ps in my case) in Java? The loop I wrote:

while( this.check )
{
    try 
    {
            ProcessBuilder pb = new ProcessBuilder("ps");
            Process proc;

            System.out.println(" * * Running `ps` * * ");

            byte[] buffer;
            String input;

            proc = pb.start();
            BufferedInputStream osInput = 
                new BufferedInputStream(proc.getInputStream());

            //prints 0 every time after the first
            System.out.println(osInput.available());

            buffer = new byte[osInput.available()];
            osInput.read(buffer);
            input = new String(buffer);
            for( String line : input.split("\n"))
            {
                if( line.equals("") )
                    continue;
                this.handlePS(line);
            }

            proc.destroy();
            try 
            {
                Thread.sleep(10000);
            } 
            catch (InterruptedException ie) 
            {
                ie.printStackTrace();
            }
        } 
        catch (IOException ioe) 
        {
            ioe.printStackTrace();
        }
    }
}

doesn’t work. It runs perfectly fine the first time, but there are 0 bytes available from the input stream every time after that. I’d try the watch command, but this Solaris box doesn’t have that. I can’t use a cron job since I need to know if the PID is there in the Java Application. Any ideas?

Thanks in advance.

EDIT: cannot use cron job

EDIT: I’m making a new Thread of the same type (PS) after it concludes, so I am definitely making a new ProcessBuilder every time.

EDIT: I put the loop that didnt work back in since it caused confusion.

  • 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-05-11T21:33:05+00:00Added an answer on May 11, 2026 at 9:33 pm

    I’m not certain where the loop is, but you will need to create a new Proc object (and thus a new InputStream) each time through the loop. Otherwise you will always be looking at the result to the first call. The javadocs for ProcessBuilder indicate that you do not need to create one of those each time.

    There may also be a race condition where the input stream is not yet ready when you callk available(). You should look at making certain that the input stream has reached EOF (which will happen with ps, although not with, say, top) before printing the results.

    You are also not handling encoding properly, although I don’t know what kind of encoding the output of “ps” is (outside of ASCII). Since “ps” is probably ASCII this is reasonably safe, but may not be for other commands (and for other input streams).

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

Sidebar

Related Questions

Is there a way to periodically run some code on app engine? I have
Is there a way to periodically run an elisp function in a long-running emacs,
Is there any way to check whether a file is locked without using a
This is my first crack at a method that is run periodically during the
I'm looking for a way to periodically (e.g. weekly) run some SQL statements in
we need run one function periodically in Java web application . How to call
Is there a way to test if a collection is already initialized? try-catch only?
Is there way in next piece of code to only get the first record?
is there way thats i can preselect an item when the page loads or
Is there a way to enforce constraint checking in MSSQL only when inserting new

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.