Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Runtime and process picking up unwanted char's

Thread view: 
iksrazal@gmail.com - 15 Mar 2006 19:52 GMT
Hi all,

I was looking at a problem at comp.os.linux.misc - trying to put
'pgrep' thru 'wc -w' . Allow the code below to explain:

package org.client;

import java.util.*;
import java.io.*;

public class Pgrep {

   public static void main(String[] args) {
       try {
           // String[] cmd = new String[]{"sh", "-c", "/usr/bin/pgrep
-f konqueror | /usr/bin/wc -w"
           // String[] cmd = new String[]{"sh", "-c", "/usr/bin/pgrep
-f konqueror"
           String[] cmd = new String[]{"sh", "-c", "/usr/bin/pgrep -f
konqueror | od -c"
           };

         System.out.println(cmd[0] + " " + cmd[1] + " " + cmd[2]);
         Runtime rt = Runtime.getRuntime();
         Process proc = rt.exec(cmd);
         // put a BufferedReader on the ls output

         InputStream inputstream =
             proc.getInputStream();
         InputStreamReader inputstreamreader =
             new InputStreamReader(inputstream);
         BufferedReader bufferedreader =
             new BufferedReader(inputstreamreader);

         // read the ls output

         String line;
         while ((line = bufferedreader.readLine())
                   != null) {
             System.out.println(line);
         }

         // check for failure
         if (proc.waitFor() != 0) {
                 System.err.println("exit value = " +
                     proc.exitValue());
         }
       } catch (Exception ex) {
           ex.printStackTrace();
       }
   }

}

>From the shell I have:

/home/iksrazal/ahelp> ps -ef | grep [k]onqueror
iksrazal 23848  7744  0 Mar14 ?        00:01:05 konqueror [kdeinit]
konqueror --silent
iksrazal 29474  7744  0 Mar14 ?        00:00:00 kio_file [kdeinit]
kio_file file /tmp/ksocket-iksrazal/klauncherkxpgOb.slave-socket
/tmp/ksocket-iksrazal/konquerorRuDO7b.slave-socket

/home/iksrazal/ahelp> /usr/bin/pgrep -f konqueror | wc -w
2

Running from the above program - with the command above running 'wc -w'
I get 3.  Running the version with 'od -c' I get:

    [echo] running grep
    [java] sh -c /usr/bin/pgrep -f konqueror | od -c
    [java] 0000000   2   3   8   4   8  \n   2   9   4   7   4  \n   1
 7   8   2
    [java] 0000020   7  \n
    [java] 0000022

With the content after '23848' and '29474'  changing with every
invokation.

Any ideas?

Robert
http://www.braziloutsource.com/
iksrazal@gmail.com - 15 Mar 2006 20:17 GMT
> Hi all,
>
> I was looking at a problem at comp.os.linux.misc - trying to put
> 'pgrep' thru 'wc -w' . Allow the code below to explain:

It just occurred to me. Might the java program itself be picking up the
'pgrep' process looking for 'konqueror' ?  :-) .

Robert
http://www.braziloutsource.com/index.html
Gordon Beaton - 16 Mar 2006 10:03 GMT
> It just occurred to me. Might the java program itself be picking up
> the 'pgrep' process looking for 'konqueror' ? :-) .

More likely your grep/pgrep is detecting itself due to the matching
argument.

Is there a Java issue here?

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

iksrazal@gmail.com - 16 Mar 2006 13:41 GMT
> > It just occurred to me. Might the java program itself be picking up
> > the 'pgrep' process looking for 'konqueror' ? :-) .
[quoted text clipped - 5 lines]
>
> /gordon

The java issue is that the pgrep command as written returns 3 with java
while the same command in the shell returns 2. See the OP if
interested. While grep detects itself by default, using [] with it as
my example shows it does not. Neither does pgrep.

Robert
Gordon Beaton - 16 Mar 2006 14:12 GMT
> The java issue is that the pgrep command as written returns 3 with
> java while the same command in the shell returns 2. See the OP if
> interested. While grep detects itself by default, using [] with it
> as my example shows it does not. Neither does pgrep.

The original post shows that [] was *not* being used in the command
passed to Runtime.exec(), only in the shell examples.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

iksrazal@gmail.com - 16 Mar 2006 16:04 GMT
> > The java issue is that the pgrep command as written returns 3 with
> > java while the same command in the shell returns 2. See the OP if
[quoted text clipped - 5 lines]
>
> /gordon

I know you're trying to help - and in fact you have helped me in the
past. What I meant by 'Neither does pgrep' - and I should have been
more clear - is that pgrep _does not_ in any way list itself in the
results as does the default behavior or grep.

/home/iksrazal> ps -ef | grep konqueror
iksrazal 23848  7744  0 Mar14 ?        00:01:08 konqueror [kdeinit]
konqueror --silent
iksrazal 18905     1  0 Mar15 ?        00:01:41 konqueror [kdeinit]
konqueror -mimetype text/html http://fortaleza.techdays.soujava.org.br
iksrazal 29806  7744  0 10:04 ?        00:00:00 kio_file [kdeinit]
kio_file file /tmp/ksocket-iksrazal/klauncherkxpgOb.slave-socket
/tmp/ksocket-iksrazal/konquerorlPtb2b.slave-socket
iksrazal 31765 10406  0 11:55 pts/2    00:00:00 grep konqueror

[linux(iksrazal)]
/home/iksrazal> ps -ef | grep [k]onqueror
iksrazal 23848  7744  0 Mar14 ?        00:01:08 konqueror [kdeinit]
konqueror --silent
iksrazal 18905     1  0 Mar15 ?        00:01:41 konqueror [kdeinit]
konqueror -mimetype text/html http://fortaleza.techdays.soujava.org.br
iksrazal 29806  7744  0 10:04 ?        00:00:00 kio_file [kdeinit]
kio_file file /tmp/ksocket-iksrazal/klauncherkxpgOb.slave-socket
/tmp/ksocket-iksrazal/konquerorlPtb2b.slave-socket

[linux(iksrazal)]
/home/iksrazal> pgrep konqueror
23848
18905

(java program, all three 'cmd' options shown in OP code)

[echo] running grep
    [java] sh -c /usr/bin/pgrep -f konqueror | od -c
    [java] 0000000   2   3   8   4   8  \n   1   8   9   0   5  \n   2
 9   8   0
    [java] 0000020   6  \n   3   2   0   1   8  \n
    [java] 0000030

Where is ' 3   2   0   1   8' comming from?

[echo] running grep
    [java] sh -c /usr/bin/pgrep -f konqueror | /usr/bin/wc -w
    [java] 4

[echo] running grep
    [java] sh -c /usr/bin/pgrep -f konqueror
    [java] 23848
    [java] 18905
    [java] 29806

Robert
Gordon Beaton - 16 Mar 2006 16:33 GMT
> (java program, all three 'cmd' options shown in OP code)
>
[quoted text clipped - 6 lines]
>
> Where is ' 3   2   0   1   8' comming from?

Try this in a command shell:

 [linux]$ sh -c "/usr/bin/pgrep -fl konqueror | cat"

("cat" output is easier to read than od -c for this)

You will find that pgrep is matching the sh process (with all of its
arguments). It seems that pgrep is smart enough to remove itself from
the list, and even sh when you don't use any redirection. However the
redirection and extra command throw it off. Specifing -l helps you see
which processes are being reported. The extra process is this one:

 32018 sh -c /usr/bin/pgrep -fl konqueror | cat

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

iksrazal@gmail.com - 16 Mar 2006 18:19 GMT
> > Where is ' 3   2   0   1   8' comming from?
>
[quoted text clipped - 13 lines]
>
> /gordon

That explains it! Thanks Gordon,

Robert
http://www.braziloutsource.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.