通过 ssh 使用 watch

sus*_*ush 6 linux ssh shell watch

我有以下命令在本地计算机上正常运行。

watch -t -n1 "echo `date '+%Y-%m-%d %H:%M:%S'` | tee -a Time.txt" &>/dev/null &
Run Code Online (Sandbox Code Playgroud)

但是当我从远程计算机运行它时,我不会创建预期的输出,即不会创建 Time.txt 并且不会作为后台进程运行。

ssh ipaddress watch -t -n1 "echo `date '+%Y-%m-%d %H:%M:%S'` | tee -a Time.txt" &>/dev/null &
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题。我尝试了多种选项,例如将“,”作为 watch 命令,但它并没有帮助我。

cev*_*ing 6

  • 无需使用echo子 shell 来输出结果。
  • 无需使用tee附加到文件。
  • 无需使用watch睡眠一秒钟。

试试这个吧。

ssh -t ipaddress 'while sleep 1; do date +%Y-%m-%d\ %H:%M:%S >> Time.txt; done &'
Run Code Online (Sandbox Code Playgroud)


Ben*_*Ben 5

您的 shell 语法不正确:explainshell

并在运行命令时抛出此错误:

Error opening terminal: unknown.
Run Code Online (Sandbox Code Playgroud)

你应该使用该选项-t

-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.
Run Code Online (Sandbox Code Playgroud)

并引用整个命令并将其传递给 ssh,并在必要时转义字符。附解释shell

ssh -t ipaddress 'watch -t -n1 "echo `date +%Y-%m-%d\ %H:%M:%S` | tee -a Time.txt" \&\>/dev/null \&'
Run Code Online (Sandbox Code Playgroud)