简体   繁体   中英

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 . 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.

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.

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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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