简体   繁体   English

打开终端失败:缺少或不合适的终端:从 Java 程序运行 shell 脚本时未知

[英]open terminal failed: missing or unsuitable terminal: unknown when running shell script from Java program

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 .我有一个 Java 程序运行一个带有进程的 shell 脚本,但是由于某种原因,当我尝试运行它时,它会抛出一个错误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.从其他 SO 问题来看,我认为这是一个 tmux 问题,但我不确定如何解决它。 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:这是shell脚本:

#! /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.我试过从终端运行脚本,它可以从终端运行,但是当我运行 Java 程序时它不起作用。

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.问题是您正在附加到交互式tmux会话,您需要有一个支持光标移动代码等的终端。

The easy workaround is to not attach;简单的解决方法是不附加; just submit the command you want to run into the session.只需将要运行的命令提交到会话中即可。

ssh -tt -i ~/.ssh/ssh-key.key opc@___._.___.___ tmux send-keys './run' C-m

This obviously requires that whatever is running inside the remote tmux session is in a state where you can submit a shell command, ie at a shell prompt or similar.这显然要求远程tmux会话中运行的任何东西都处于可以提交 shell 命令的状态,即在 shell 提示符或类似提示下。 For robustness you might want to take additional measures to make sure this is always the case, or refactor your solution to avoid running inside tmux eg 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).为了稳健性,您可能需要采取额外措施来确保始终如此,或者重构您的解决方案以避免在tmux内部运行,例如通过让命令将其输出重定向到一个文件,您可以随时从任何终端检查它(尽管这假设您随后也不需要与之交互)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM