开放终端失败:丢失或不合适的终端:从Java程序运行Shell脚本时未知
我有一个带有一个流程的Shell脚本的Java程序,但是由于某些原因,当我尝试运行它时,它会引发错误开放终端失败:丢失或不合适的终端:Unknownapent
。从其他问题来看,我认为这是一个TMUX问题,但我不确定如何解决。这是调用脚本的代码:
ProcessBuilder pb = new ProcessBuilder("/Users/user/eclipse-workspace/project/start.sh");
Process p = pb.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println("output: ");
String s;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
这是shell脚本:
#! /bin/sh
ssh -tt -i ~/.ssh/ssh-key.key opc@___._.___.___ tmux attach -d << END
./run.sh
END
exit 0
我尝试从终端运行脚本,并且它可以从终端运行,但是当我运行Java程序时它不起作用。
I had a Java program running a shell script with a Process, but for some reason when I try to run it it throws an error open terminal failed: missing or unsuitable terminal: unknown
. From other SO questions, I think this is a tmux problem, but I'm not really sure how to solve it. Here's the code calling the script:
ProcessBuilder pb = new ProcessBuilder("/Users/user/eclipse-workspace/project/start.sh");
Process p = pb.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println("output: ");
String s;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
And here's the shell script:
#! /bin/sh
ssh -tt -i ~/.ssh/ssh-key.key opc@___._.___.___ tmux attach -d << END
./run.sh
END
exit 0
I have tried running the script from terminal, and it works from the terminal but it doesn't work when I run the Java program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,您将附加到交互式
tmux
会话中,其中您需要拥有一个支持光标运动代码等的终端等。只需提交您要在会话中遇到的命令即可。
这显然要求在远程
tmux
会话中运行的任何内容处于一个状态,您可以在Shell提示或类似的情况下提交Shell命令,即IE。为了鲁棒性,您可能需要采取其他措施以确保始终是这种情况,或者对解决方案进行重构,以避免在tmux
中运行,例如,命令将其输出重定向到文件,您可以在其中检查它从任何时候从任何终端开始(尽管这是您也不需要随后与它进行交互)。The problem is that you are attaching to an interactive
tmux
session, where you need to have a terminal which supports cursor movement codes etc.The easy workaround is to not attach; just submit the command you want to run into the session.
This obviously requires that whatever is running inside the remote
tmux
session is in a state where you can submit a shell command, i.e. at a shell prompt or similar. For robustness you might want to take additional measures to make sure this is always the case, or refactor your solution to avoid running insidetmux
e.g. by having the command redirect its output to a file where you can examine it from any terminal at any time (though this assumes you don't also need to interact with it subsequently).