Runtime and process picking up unwanted char's

I

iksrazal

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/
 
G

Gordon Beaton

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
 
I

iksrazal

Gordon Beaton escreveu:
More likely your grep/pgrep is detecting itself due to the matching
argument.

Is there a Java issue here?

/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
 
G

Gordon Beaton

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
 
I

iksrazal

Gordon Beaton escreveu:
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

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
 
G

Gordon Beaton

(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?

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
 
I

iksrazal

Gordon Beaton escreveu:
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

That explains it! Thanks Gordon,

Robert
http://www.braziloutsource.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top