Solved

stdout missing after exec()

Posted on 1997-06-02
6
671 Views
Last Modified: 2013-11-18
I am trying to rewrite a command scheduler
from a Korn shell version in Java. The scheduler
maintains a database of what commands need to
be executed, and in what sequence, etc.

I have ported the guts of the program and it works.
However, it needs to log the output from each of
the commands it executes. The problem is that I
don't understand how to get the Process.exec() method
to send the output of the called command to where I
would expect (standard out).

The following snippet of code illustrates the problem:

//----------------------------------------------------------------------

class Ecmd {

public static void main(String args[]) {

        String cmd="";

        Ecmd ecmd = new Ecmd();

        for (int i=0 ; i<args.length ; i++)
                {
                cmd=cmd+args[i]+" ";
                }

        System.out.println("cmd: "+cmd);

        ecmd.exec_cmd(cmd);

}

private boolean exec_cmd(String cmd)
        {

        // String       cmd="/usr/bin/echo /tmp/java.ksh >/tmp/java.out 2>&1";

        Runtime ecmdRuntime=Runtime.getRuntime();
        Process ecmdProcess;

        System.out.println("trying: "+cmd);

        try { ecmdProcess = ecmdRuntime.exec(cmd); }
        catch (IOException ex) {
                System.out.println("ERROR: cannot exec : "+cmd+" value of ex:"+ex);
                System.out.println("ex: "+ex);
                return false;
                }
        try { ecmdProcess.waitFor(); }
        catch (InterruptedException ex) {
                System.out.println("ERROR: InterruptedException ");
                System.out.println("ex: "+ex);
                return false;
                }
        System.out.println("Value of ecmdProcess.exitValue(): "+ecmdProcess.exitValue());
        if ( ecmdProcess.exitValue() == 0 )
                return true;
        else
                return false;
        }

}

//----------------------------------------------------------------------

If I create the file /tmp/java.ksh and the contents of this file
are:

#!/bin/ksh
print "executing $0"
if [[ $# != 0 ]]
then
        print "executing $0" >/tmp/$1
fi


I can then run the above Java code as:

> java Ecmd /tmp/java.ksh test_output

I can verify that the code 'works' because it creates
the file /tmp/test_ouput and it contains the string 'executing /tmp/java.ksh'

But I don't know what is happening to the standard out or standard
error from /tmp/java.ksh.

I would have assumed  that the process created would inherit the standard
out and standard in and standard error from the executing shell. But
I have tried several different means of trying to redirect the output
from the called command (/tmp/java.ksh) but with no success.

Any ideas would be appreciated.

Thanks,
John Sillers

--------------------------------------------------------------------------------
John Sillers                    AIX Build Architecture & Integration
11501 Burnet Road               PHONE:    512-838-1007 or t/l 678-1007
2F05/905 Zip 9530               INTERNET: jsillers@austin.ibm.com
Austin, TX 78758                Opinions expressed are my own.

0
Comment
Question by:jsillers
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1221128
Try executing the command in its own shell:

// String cmd="/bin/sh /usr/bin/echo /tmp/java.ksh >/tmp/java.out 2>&1";

In theory at least, that would launch it in a clean environment.
0
 

Author Comment

by:jsillers
ID: 1221129
I t looks like the results are the same. I can execute a command in its
own shell or not and either way the standard output is missing.
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1221130
Strange,

Which version of UNIX are you using?

The VM is supose to run ontop of the shell, the exec command forks and spawns a new shell which should work out normaly. This leads me to belive the problem is more likley related to the OS than to Java.

Could you post your ,profile ? Also, try tee 'ing the output to another file.
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:jsillers
ID: 1221131
I am using AIX.

It turns out the question was answered in comp.lang.java.programmer (thank you steva Zeiger). What I needed to do was to create a source for the standard out:

src_stdout = dbldProcess.getInputStream();

and copy that to a FileOutputStream.

The exec command does seem to have forke a new shell which has inherited the environment of the shell running the java command. However, the process that is exec'ed has its own stdout, stderr and stdin which can be manipulated. This turns out to be good for my application because that is sort of the point. I need to have the application direct output to a log file. I would not want the standard out to HAVE to go to the same standard out as the shell which invoked it.
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1221132
Great! glad you got an answer for your problem.

I suggest you post a message to the customer support forum,  inform them that you found an answer to your question elswhere  and to cancel this question. That way you can get your points back and use em for something else:-)
0
 

Accepted Solution

by:
turon earned 100 total points
ID: 1221133
A good thing to try would be to execute your DOS program
from the first ever 100% java shell library:

zaBava.shell: http://www.zaBava.com/ftp/zapShell.zip 

Unzip the above package into your classpath, then run:
          % java zaBava.shell.main

Once inside the zaBava.shell (a pure java app),
execute your command by typing its name:

          z![~]> /usr/bin/echo /tmp/java.ksh

The next release of the zaBava.shell will feature true
multi-threaded output streams and JDK 1.1.2 compatibility.
The current version is JDK 1.0.2 compliant.

Hope the code helps you,
   ---== Martian ==---
           !!!
            |
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java JRE greater than 1.6 5 62
Basic Java Case or If-Else statement... 3 50
eclipse buid path vs tomcat lib path 10 23
Android development question 2 37
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to dynamically set the form action using jQuery.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question