Reputation: 23
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
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
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