Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running a program through ssh fails with "Error opening terminal: unknown."

When I try to execute a simple command through ssh, then it is successful. e.g.

#] ssh servername "echo abcd"
abcd
#] 

However, when I try the following command, it fails:

#] ssh servername  ~/htopmem.sh
Error opening terminal: unknown.
#] 

where the content of htopmem.sh is below. (inspired by the answer of Marwan Alsabbagh on htop output to human readable file)

#!/bin/bash
echo q | htop | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | ~/aha --black --line-fix | grep Mem | grep -E -o "[0-9]+/[0-9]+"

If I manually ssh to the server and run htopmem, then the execution is successful:

#] ./htopmem.sh
6515/24021
#] 

any idea on how to make the "ssh servername ~/htopmem.sh" command work?

Thank you!

like image 812
Yuval Atzmon Avatar asked May 26 '15 22:05

Yuval Atzmon


1 Answers

A plain ssh command like that does not have a tty (terminal). Use the -t option to force ssh to open the terminal on its way in.

From the manual:

-t

Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g., when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

So this would work (better):

ssh -t servername  ~/htopmem.sh
like image 149
Thomas Dickey Avatar answered Oct 01 '22 03:10

Thomas Dickey