This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes chmod command executing fine but sh command not executing the script in remote server. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "chmod command executing fine but sh command not executing the script in remote server." Watch "chmod command executing fine but sh command not executing the script in remote server." New topic
Author

chmod command executing fine but sh command not executing the script in remote server.

Prem Swaroop
Greenhorn

Joined: Apr 11, 2012
Posts: 8
Hi,

I am using Ganymed-ssh2 library for connecting to a remote server and trying to execute a shell script there. I am first transferring a script, then giving all permissions with chmod and then executing it. I am able to execute chmod but unable to execute sh command. Below is the code I used :-

public synchronized boolean runScript(String remoteFile)
{
try
{
Connection conn = new Connection(host);
conn.connect();
boolean isAuth = conn.authenticateWithPassword(user,password);
Session sess = conn.openSession();
sess.execCommand("/usr/bin/chmod 777 " + remoteFile + ".sh"); //This is working as expected.
sess.close();
sess = conn.openSession();
sess.execCommand("/usr/bin/sh " + remoteFile + ".sh"); // This is the problematic part for me.
InputStream stdout = new StreamGobbler(sess.getStdout());
InputStream stderr = new StreamGobbler(sess.getStderr());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
BufferedReader brErr = new BufferedReader(new InputStreamReader(stderr));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while( line != null )
{
sb.append( line + "\n" );
line = br.readLine();
}
String lineErr = brErr.readLine();
while( lineErr != null )
{
sb.append( lineErr + "\n" );
lineErr = brErr.readLine();
}
sess.close();
conn.close();
return true;
}
catch(Exception e)
{
StringWriter sw0= new StringWriter ();
PrintWriter p0= new PrintWriter ( sw0, true );
e.printStackTrace ( p0 );
erMesg = sw0.getBuffer().toString();
return false;
}
}

And this method returns true that means execCommand is not throwing any exception. But the script is supposed to display some output on the screen and it also creates another file. Neither of these two happens.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Not sure why you close and then open connection again between the 2 execCommand() calls? Does /usr/bin/sh exist on the remote server? Could you run the 2 commands on one line like this:

In your remote script, try and write something to a file like this: and check if this file gets created on the remote server. It's possible you don't see screen output if no console (tty) is associated to your remote session.
Prem Swaroop
Greenhorn

Joined: Apr 11, 2012
Posts: 8
Thanks for your reply Koen. I didn't know to execute 2 different commands in one statement. Let me try running the 2 commands in one line as said by you.

/usr/bin/sh exists in my remote server. which sh --> /usr/bin/sh

I will also check if any file gets created by running shell script from remote server.

Basically I was collecting the output in StringBuilder from stdout and stderr. I created a TextArea where I am passing the StringBuilder. But not getting any output.

I will try the above steps any will reply back.

Thanks,
Prem.
Prem Swaroop
Greenhorn

Joined: Apr 11, 2012
Posts: 8
I created a simple program just only to test whether I am able to execute a script in my remote server. So It executed as expected. Also I am able to create a new file.

But in my main program my shell script contains "FNDLOAD" command which is an Oracle Utility. I did not mention the absolute path of FNDLOAD, that may be the reason that script didnt executed as expected. Although it didnot catch any exception. Also, I didnt get any error message, as I used getStderr() to collect the error if any in my TextArea.

So what I concluded from this is StreamGobbler is only used to get the output and error of any unix commands but not Oracle commands.

I will try now giving the absolute path of "FNDLOAD" and see if there is any luck.

Please correct me if I am wrong.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

The environment settings for the script may not be the same as those in a regular (terminal) session, for instance because .profile, .bashrc, or whatever other post-logon script is normally only executed after regular terminal logins. To test this, you could try a "env > /tmp/myenv.txt" in your script, then execute your script manually from a terminal and again through your remote program, then compare the two outputs from the "env" command. Likely they're different. So either your script either has to set required variables itself, or execute your default logon script (i.e. .profile, .bashrc, ... depending on the shell you're using, which you can find in /etc/passwd) and assuming it's using the same user account. Also, in the first line of your script you can specify which shell it needs to run in, for instance like "#!/usr/bin/sh". So then instead of "/usr/bin/sh script.sh" you just call your script like "script.sh" and it will still use /usr/bin/sh.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: chmod command executing fine but sh command not executing the script in remote server.
 
Similar Threads
Runtime getRuntime() exec(cmd[]) - How does this work?
runnning a shell script from mac os x
Run the Shell script in java
How to display InputSream dynamically on a JSP page?
How to update a JTextArea in real time?