从shell命令的Java code获取响应 [英] Getting response from shell command to Java code

查看:298
本文介绍了从shell命令的Java code获取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SSH net.neoremind.sshxcute的Java API库连接到SFTP服务器,并且服务器上执行shell脚本present。

我的Shell脚本确实从SFTP位置文件移动到HDFS的位置在其它机器上的一个简单的工作。

目前,有没有办法来报告如果任何文件都不会被移动,由于某种原因,如连接失败,非法名字的文件,空文件等。

我不知道,我怎么能显示的每个信息集从失败的shell命令文件移动回Java code?

这是我的样本code:

  //例sftpScriptPath => / ABC / XYZ
// sftpScriptCommand =>须藤./move.sh
//参数=>设置的参数shell脚本。
任务=新ExecShellScript(sftpScriptPath,sftpScriptCommand,参数);
结果= m_SshExec.exec(任务);
如果(result.isSuccess&安培;&安培; result.rc == 0)
{
     isSuccessful =真;
     s_logger.info(shell脚本执行成功);
     s_logger.info(返回code:+ result.rc);
     s_logger.info(SYSOUT:+ result.sysout);
}
其他
{
     isSuccessful = FALSE;
     s_logger.info(shell脚本执行失败);
     s_logger.info(返回code:+ result.rc);
     s_logger.info(SYSOUT:+ result.sysout);
}


解决方案

的<一个href=\"http://$c$c.google.com/p/sshxcute/source/browse/trunk/sshxcute/src/net/neoremind/sshxcute/core/Result.java\"相对=nofollow>结果对象从 EXEC 方法调用包括:


  • 退出状态或返回code( Result.rc ),

  • 标准输出(stdout)( Result.sysout ),

  • 标准错误(错误)( Result.error_msg )和

  • 成功的标志,<一个href=\"http://$c$c.google.com/p/sshxcute/#4.1_Sysout_keywords_to_determine_whether_task_is_successful_or_n\"相对=nofollow>基于返回code和输出( Result.isSuccess )。

所以,如果你正致力于使用 sshxcute 框架,那么最简单的方法是将有 move.sh 脚本提供有关任何失败的信息,而移动文件。这可以通过return codeS和标准输出(stdout)和/或标准错误(错误)信息的组合来完成。那么你的Java code将获得从返回的<一个信息href=\"http://$c$c.google.com/p/sshxcute/source/browse/trunk/sshxcute/src/net/neoremind/sshxcute/core/Result.java\"相对=nofollow>结果对象。

I'm using net.neoremind.sshxcute SSH Java API library to connect to a sftp server and execute a shell script present on that server.

My Shell Script does a simple job of moving files from that SFTP location to a HDFS location on some other machine.

Currently, there's no way to report if any of the files are not moved due to any reason such as connection failure, file with illegal name, empty file etc.

I wonder, how can I show that set of information for each failed file move from shell command back to Java code ?

This is my sample code :

// e.g sftpScriptPath => /abc/xyz
// sftpScriptCommand => sudo ./move.sh
// arguments => set of arguments to shell script.
task = new ExecShellScript(sftpScriptPath, sftpScriptCommand, arguments);
result = m_SshExec.exec(task);
if(result.isSuccess && result.rc == 0)
{
     isSuccessful = true;
     s_logger.info("Shell script executed successfully");
     s_logger.info("Return code : " + result.rc);
     s_logger.info("Sysout : " + result.sysout);
}
else
{
     isSuccessful = false;
     s_logger.info("Shell script execution failed");
     s_logger.info("Return code : " + result.rc);
     s_logger.info("Sysout : " + result.sysout);
}

解决方案

The Result object returned from the exec method call includes:

  • exit status or return code (Result.rc),
  • standard output (stdout) (Result.sysout),
  • standard error (stderr) (Result.error_msg), and
  • an indication of success, based on return code and output (Result.isSuccess).

So, if you are committed to the current method of executing a shell script using the sshxcute framework, then the simplest way would be to have the move.sh script provide information about any failures while moving files. This could be done via a combination of return codes and standard output (stdout) and/or standard error (stderr) messages. Your Java code would then obtain this information from the returned Result object.

这篇关于从shell命令的Java code获取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆