How to execute sh file of unix in Java
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   surya_Hota
Posted On:   Thursday, October 4, 2007 08:29 AM

iam trying to execute a sh file in target machine which will startup the database from source machine.. ** Iam totally working on unix platform .. ** can any one also specify the use of && in unix.. ** My java program hangs out after executing the unix command with out continuing to the next command.. **Iam sending only some part og the program.. **The Remaining part contains the input & error streams.. sh file is .. # Synopsys # -------------------------------------------------------------------------------- # This program startup's the database. # # Para   More>>

iam trying to execute a sh file in target machine which will startup the database from source machine..

** Iam totally working on unix platform ..

** can any one also specify the use of && in unix..

** My java program hangs out after executing the unix

command with out continuing to the next command..

**Iam sending only some part og the program..
**The Remaining part contains the input & error streams..


sh file is ..

# Synopsys
#


--------------------------------------------------------------------------------
# This program startup's the database.
#

# Parameters

#

--------------------------------------------------------

------------------------

# @1: tuserconnect

# @2: tenvfile

# @3: tUserHome

#

progname=ajstartdb

echo "ssh ${1} . ${2} && ${3}"

ssh ${1} . ${2} && ${3}/ajss/sql/ajstrtdb.sh

retval=$?

echo "DEBUG::${progname}--> Completing execution

with return code: $retval" > $progname.out

If i try to execute this ajstartdb.sh file in target itself it is executing perfectely.So i think no prob's with sh file..

but when i try to execute this sh file from java.. the process control is remaining in the prgm itself even after the cmd is executed.. i mean prgm is not exiting.

java prgm is :

import java.io.*;

class StartShut1

{

public static void main(String[] args)
{

try {

String cmd="ssh

oragold@hydapps.solix.com . /d03/AppsGOLD/golddb/9.2.0/GOLD_hydapps.env &
& /home/oragold/ajss/ajstrtdb.sh";

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

Process p =Runtime.getRuntime().exec(cmd);

int i = p.waitFor();

System.out.println("Exit value of process is "+j);

System.out.println("Output of the Cmd is : "+i);

String s=null;

if (i!=0){

System.out.println("ERROR in executing the command");

BufferedReader cmdr = new BufferedReader(new InputStreamReader(p.getErrorStream()));

while ((s = cmdr.readLine()) != null) {

System.out.println(s);

}

}

s = null;

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

// read the output from the command

s=stdInput.readLine();

System.out.println(s);

while (s!= null)

{

System.out.println(s);

}

InputStream in = p.getInputStream();

int ch;

StringBuffer sb = new StringBuffer(512);

while ( ( ch = in.read() ) != -1 )

{ sb.append((char) ch); }

System.out.println("output from string buffer"+sb);

}catch(Exception e)

{

System.out.println("ERROR :"+e);

e.printStackTrace();

}

}//main

}//class

Kindly Reply me if u get an idea










.

   <<Less

Re: How to execute sh file of unix in Java

Posted By:   Robert_Lybarger  
Posted On:   Thursday, October 4, 2007 10:25 PM

The first snafu to sort out when you exec something that emits output (either stdout or stderr) is that said process might get hung up when its output buffer is full. In other words, you may need to either (a) open those streams and clear them out yourself (which tends to involve a thread or two) or (b) modify the command you are running to redirect output and error streams somewhere, usually to a file (or possibly to /dev/null if you honestly don't care to ever see it). My sh is rusty at the moment ... I think you'd need to add > /some/file.txt 2&>1 to the end of the command being executed. (Consult a man page or a good book.) Aside: if you use the ProcessBuilder class to get things set up, you can tell it to combine the error stream with the output stream. Check the API docs for the right method call to do that.
About | Sitemap | Contact