java 调用shell的问题。高分求高手赐教

greki 2010-12-20 11:04:12
问题如下:

String str[]={"/bin/sh","-c","sh test1.sh"};
Process pcs=rt.exec(str);

String str[]={"/bin/sh","-c","sh test2.sh"};
Process pcs=rt.exec(str);

A、B测试环境运行一切OK,C环境调用失败(2个shell都失败)。

改成
Process pcs=rt.exec("sh test2.sh");
Process pcs=rt.exec("sh test1.sh");
A、B环境OK,C环境test1.sh成功。调用test2.sh失败。。test2.sh直接命令运行可以成功。运行命令的用户和应用的用户一样。

test1.sh 跟 test2.sh 区别只有test2.sh需要输入一个密码。(现在没有shell,明天来贴源码)
大致test2.sh类似: gpg xxxxx </home/password.txt 是个gpg解密


现在很纠结。3个环境,.sh的权限。文件输出目录的权限都正确。密钥配置也正确,找到原因出在哪里
请大家帮想想可能的原因。


...全文
132 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
greki 2010-12-21
  • 打赏
  • 举报
回复
没有返回
/usr/local/bin/gpg --no-tty --passphrase-fd 0 --yes -o $2 -d $1 < /home/admin/bops/Password.txt 2> /tmp/temp.txt

现在解决了。C环境找不到/dev/tty

运行的时候加了 --no-tty

谢谢各位了
qybao 2010-12-21
  • 打赏
  • 举报
回复
这个阻塞判断不了
执行后return sb.toString();返回的信息是什么?

greki 2010-12-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qybao 的回复:]
就两行?
$1 $2参数没传入也不影响吗?
chmod +x test2.sh 直接Process pcs=rt.exec("test2.sh");试试看
难不成发生了process的死锁?
javadoc的Process的说明的倒数第三段的最后一句话有关于死锁方面的说明

Because some native platforms only provide limited buffe……
[/Quote]
什么方法判断是否阻塞了
greki 2010-12-21
  • 打赏
  • 举报
回复
if (StringUtil.isBlank(cmd)) {
throw new Exception("the arg cmd is null");
}
Process process = null;
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String s = null;
try {
//TODO debug 改成linux
process = Runtime.getRuntime().exec(cmd);
//process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", cmd });
//process = Runtime.getRuntime().exec("cmd /C start /min "+ cmd );
log.error("====cmd===start==== "+ cmd);
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((s = br.readLine()) != null) {
sb.append(s).append("\n");
}
log.error("====cmd===end======================= ");
log.error("====result:======="+sb.toString());
//if(doSynchrony){
//process.wait();
//}
return sb.toString();
// }catch(InterruptedException e){
// log.error("execute cmd error,processes was interrupted, cmd: " + cmd);
// throw e;
}
catch(Exception e){
log.error(e);
throw e;
}
finally {
if (process != null) {
process.destroy();
}
process = null;
if (br != null) {
try {
br.close();
} catch (Exception e) {
throw e;
}
br = null;
}
}
greki 2010-12-21
  • 打赏
  • 举报
回复
先谢谢各位。
Linux jp_home6 2.4.21-51.EL3.customsmp #1 SMP Fri Jun 27 10:44:12 CST 2008 i686 i686 i386 GNU/Linux

[admin@jp_home6 bops]$ echo $0
/bin/bash
yangxiao_jiang 2010-12-21
  • 打赏
  • 举报
回复
应该是环境的问题,你看看jdk的版本,环境变量,登陆用户的时候,是不是在profile里有设置变量了
qybao 2010-12-21
  • 打赏
  • 举报
回复
就两行?
$1 $2参数没传入也不影响吗?
chmod +x test2.sh 直接Process pcs=rt.exec("test2.sh");试试看
难不成发生了process的死锁?
javadoc的Process的说明的倒数第三段的最后一句话有关于死锁方面的说明

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

Jlins 2010-12-21
  • 打赏
  • 举报
回复
/bin/sh"
是sh 还是 ksh 或者其他的?
greki 2010-12-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qybao 的回复:]
shell中有用到环境变量什么的吗?
运行失败有什么错误提示吗?
[/Quote]

test2.sh

DIRNAME=`dirname $0`
gpg --passphrase-fd 0 --yes -o $2 -d $1 < $DIRNAME/Password.txt

没有提示
qybao 2010-12-20
  • 打赏
  • 举报
回复
shell中有用到环境变量什么的吗?
运行失败有什么错误提示吗?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧