Solved

stdout missing after exec()

Posted on 1997-06-02
6
673 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Certificat to iSeries KeyStore 1 30
by zero exception 10 83
Desingning Refactoring existing code 2 45
Html split(text) 2 49
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

739 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