StyleTang
StyleTang

Reputation: 23

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","[email protected]" ,"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  [email protected] still has 'tail -f /log.out' running
    beego.Error(err)
}

Upvotes: 1

Views: 704

Answers (2)

Uvelichitel
Uvelichitel

Reputation: 8490

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

Upvotes: 0

faza
faza

Reputation: 26

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

var  cmd = exec.Command("ssh","-t", "-t", "-p", "9122","[email protected]" ,"tail -f /log.out")

For more information, refer this link

Upvotes: 1

Related Questions