Search code examples
gocmdtailkill-process

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


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

Solution

  • 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