go execute ssh command 并且无法在远程服务器上杀死命令

go execute ssh command and can't kill the command on the remote server

我使用 go exec ssh 在远程服务器上执行 "tail -f"。然后我终止进程,但 "tail -f " 仍在远程服务器上运行。

如何终止远程服务器上的 "tail -f" 进程?

我的代码如下:

package main

import (
    "os/exec"
    "github.com/astaxie/beego"
    "time"
)

func main() {
    var  cmd = exec.Command("ssh","-t", "-p", "9122","deploy@123.com" ,"tail -f /log.out")
    var err error
    cmd.Start()
    time.Sleep(time.Second*5)
    err = cmd.Process.Kill() // when I kill this process, the remote server  deploy@123.com still has 'tail -f /log.out' running
    beego.Error(err)
}

只需在参数中添加一个“-t”即可。

var  cmd = exec.Command("ssh","-t", "-t", "-p", "9122","deploy@123.com" ,"tail -f /log.out")

有关详细信息,请参阅此 link

您可以尝试发送Control-C

w, err := cmd.StdinPipe()
ctlC, err := hex.DecodeString(`\x03`) //CtlC on most machines
w.Write(ctlC)

在终止 ssh 之前

err = cmd.Process.Kill()

这适用于我的设置