Repeat Unix Command in Java -


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

  while (this.check) {try {ProcessBuilder pb = new process builder ("ps"); Process process; System.out.println ("* * Running ps` * *"); Byte [] buffer; String input; Proc = pb.start (); BufferedInputStream osInput = New BufferedInputStream (proc.getInputStream ()); // each time it prints after System.out.println (osInput.available ()) for the first time; Buffer = new byte [osInput.available ()]; OsInput.read (buffer); Input = new string (buffer); (String line: for input.split ("\ n")) {if (line.equals ("")) is in progress; This.handlePS (line); } Proc.destroy (); Try {Thread.sleep (10000); } Grip (ie the affected exception) {ie.printStackTrace (); }} Hold (IOException ioe) {io.printStackTrace (); }}}  

does not work This runs for the first time perfectly, but after that there are 0 bytes available from the input stream. I'll try the clock command, but this is not the case in the Solaris box. I can not use a cron job because I should know if the PID is in the Java application. Edit: Cron can not use Job

EDIT: I am creating a new thread after the expiry of the same type ( PS), so I'm definitely making a new processbilder every time.

EDIT: I put the loop which was not working to get it back, because it created illusion.

Proc .

InputStream ) otherwise you always call the result first Be looking at For process builder , Jawadox indicates that you do not need to make one of them each time.

There may also be a race situation where the input call is not ready now when you call available () . Before touching the result you should make sure that the input stream has reached the EOF (which will be with ps, though, not with it, say, above).

You are not doing the encoding properly, although I do not know how the output of "ps" is encoding (outside ASCII). Since "ps" is probably ASCII, it is quite secure, but can not be for other commands (and other input streams).


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -