I\'m establishing an SSH connection through JSch on Java and everything seemed to be working fine, until I tried to run this .sh file. The shell script\'s name is repo
I suspect that your shell command is erroring out for some reason - perhaps svn
isn't on your path, perhaps there are other weird environmental effects - but you're not getting the error output because you aren't looking for it. Normally, errors are sent on the stream you get from channelExec.getErrStream
but in your code you only read from the getOutputStream
stream.
To diagnose this, you're going to need to get those error messages. It's probably easier to get linux to use one stream for both regular output and error messages than to have your java program pulling from two streams at once, so I'd add this line as the top line of repoUpdate.sh
:
exec 2>&1
That'll then cause the rest of the script to use the one stream you're reading from as both output and error.
Also, right before you call chanelExec.disconnect
, in your java program you should record the exit status, and then change your "Done!" message based on what it was:
int exitStatus = channelExec.getExitStatus();
channelExec.disconnect();
session.disconnect();
if (exitStatus < 0) {
System.out.println("Done, but exit status not set!");
} else if (exitStatus > 0) {
System.out.println("Done, but with error!");
} else {
System.out.println("Done!");
}
If you do this, you should then get error messages that tell you why your command isn't working as you expect it should.
So here's what I did.
I added the exec 2>&1
at the top of the repoUpdate.sh file and added that piece of code to read the output error as @DanielMartin suggested, resulting on this:
public void cmremove()
{
try
{
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
UserInfo ui = new SUserInfo(pass, null);
session.setUserInfo(ui);
session.setPassword(pass);
session.connect();
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand("./repoUpdate.sh");
channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
int index = 0;
while ((line = reader.readLine()) != null)
{
System.out.println(++index + " : " + line);
}
int exitStatus = channelExec.getExitStatus();
channelExec.disconnect();
session.disconnect();
if(exitStatus < 0){
System.out.println("Done, but exit status not set!");
}
else if(exitStatus > 0){
System.out.println("Done, but with error!");
}
else{
System.out.println("Done!");
}
}
catch(Exception e)
{
System.err.println("Error: " + e);
}
}
So that actually helped a lot. It confirmed that the command was, in fact, not executing correctly as I thought. I was getting this on my java output:
run:
1 : ****Repository update****
2 : Location: /home/cissys/repo/
3 : Command: svn update /home/cissys/repo/2.3.0
4 : ./repoUpdate.sh[6]: svn: not found [No such file or directory]
Done!
BUILD SUCCESSFUL (total time: 2 seconds)
Then I tried modifying the repoUpdate.sh file, adding the absolute path for the svn command (Thank you, @ymnk)
exec 2>&1
echo ' ****Repository update****'
echo ' Location: /home/cissys/repo/'
echo -e ' Command: svn update /home/cissys/repo/2.3.0'
/opt/subversion/bin/svn update /home/cissys/repo/2.3.0
For my java, I got just what I was looking for:
run:
1 : ****Repository update****
2 : Location: /home/cissys/repo/
3 : Command: svn update /home/cissys/repo/2.3.0
4 : At revision 9443.
Done!
BUILD SUCCESSFUL (total time: 3 seconds)
I found out that the $PATH
I get from the session through java is not the same that I get directly on the linux console.
So adding the svn path actually did the trick. Thank you very much for your help!