通过JSch在java上运行linux命令 [英] Running linux commands on java through JSch
问题描述
我正在通过JSch在java上建立一个ssh连接,一切似乎工作正常,直到我试图运行这个.sh文件。 shell脚本的名称是 repoUpdate.sh ,它非常简单:
I'm establishing a 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 repoUpdate.sh and it's very simple:
echo ' ****Repository update****'
echo ' Location: /home/cissys/repo/'
echo -e ' Command: svn update /home/cissys/repo/2.3.0'
svn update /home/cissys/repo/2.3.0
这是输出我通过命令的正确响应直接进入linux控制台:
This is the output I get directly on the linux console with the proper response of the command:
[cissys@dsatelnx5 ~]$ repoUpdate.sh
****Repository update****
Location: /home/cissys/repo/
Command: svn update /home/cissys/repo/2.3.0
At revision 9432.
现在,这是我的方法的java代码,其中ssh连接尝试调用同一个文件
Now, here's the java code of my method with the ssh connection that tries to call this same file
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);
}
channelExec.disconnect();
session.disconnect();
System.out.println("Done!");
}
catch(Exception e)
{
System.err.println("Error: " + e);
}
}
我得到的回应如下:
run:
1 : ****Repository update****
2 : Location: /home/cissys/repo/
3 : Command: svn update /home/cissys/repo/2.3.0
Done!
BUILD SUCCESSFUL (total time: 2 seconds)
没有输出或执行迹象svn命令(修订版9432)无论如何。
with no output or signs of execution on the svn command (At revision 9432) whatsoever.
我认为它可能会在某个时刻关闭会话,而不是让命令正确执行。如果 updateRepo.sh 文件会有类似cp test.txt test_2.txt的内容,那么它会毫无问题地执行此操作。但我只对这个和其他一些特定的.sh文件有这个问题。
I'm thinking it may be closing the session at some point, not letting the command execute properly. If the updateRepo.sh file would've had something like "cp test.txt test_2.txt", it would do it with no problem. But i only have this problem with this and some other specific .sh files.
任何帮助都将不胜感激。
Any help would be appreciated.
推荐答案
所以这就是我所做的。
我在顶部添加了 exec 2>& 1
repoUpdate.sh 文件中添加了一段代码来读取@DanielMartin建议的输出错误,结果如下:
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);
}
}
所以这实际上帮了很多忙。它确认该命令实际上没有像我想象的那样正确执行。我在我的java输出上得到了这个:
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)
然后我尝试修改 repoUpdate.sh 文件,添加svn命令的绝对路径(谢谢,@ mysnk)
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
对于我的java,我得到了我想要的东西:
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)
我发现 $ PATH
我从会话中通过java得到的与我直接在linux控制台上得到的不一样。
所以添加svn路径实际上就可以了。非常感谢你的帮助!
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!
这篇关于通过JSch在java上运行linux命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!