Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

stdout missing after exec()

Posted on 1997-06-02
6
Medium Priority
?
679 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 200 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

704 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