running a program through ssh fails with “Error op

2019-06-18 00:45发布

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!

1条回答
劫难
2楼-- · 2019-06-18 01:26

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
查看更多
登录 后发表回答