去执行ssh命令,并且不能在远程服务器上终止该命令

I use go exec ssh to execute "tail -f" on the remote server. Then I kill the process, but the "tail -f " still runs on the remote server.

What can I do to kill the "tail -f" process on the remote server?

My code is as follows:

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

Just add one more "-t" in the arguments.

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

For more information, refer this link

You can try to send Control-C

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

before killing ssh

err = cmd.Process.Kill()

this works in my setup