Solved

stdout missing after exec()

Posted on 1997-06-02
6
677 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

628 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