Solved

stdout missing after exec()

Posted on 1997-06-02
6
669 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now