How to stop an infinite ssh command session ?

1,025 views
Skip to first unread message

Jonas Riedel

unread,
Jan 4, 2015, 4:30:56 PM1/4/15
to golan...@googlegroups.com
Hi,

i have a ssh connection and do an session.Run("infinite command") in a go routine, how can i stop this session ?

Kind of:

client, err = ssh.Dial("tcp", server.Name+":22", c.ssh)

session, err = client.NewSession()

go func() {
  if err := session.Run("long running command"); err != nil {
 }
}()



Thanks,

Jonas

Dave Cheney

unread,
Jan 4, 2015, 4:32:22 PM1/4/15
to golan...@googlegroups.com
Keep a reference to the session, close it.

Jonas Riedel

unread,
Jan 5, 2015, 3:08:29 PM1/5/15
to golan...@googlegroups.com
I've already tried this, but it does not work.

i do a 

session.Run("tail -f textfile")

in a go routine. Then i call session.Close() but the Run does not exit.

The Run does not return until the content of the textfile changes, for example when i do a echo "test">>textfile on another console.Then the Run exits with error

&ssh.ExitError{Waitmsg:ssh.Waitmsg{status:141, signal:"", msg:"", lang:""}}

seems SIGPIPE....

Any further hints ? Seems i am doing something wrong ?!?

Thanks,

Jonas

Dave Cheney

unread,
Jan 5, 2015, 3:33:54 PM1/5/15
to golan...@googlegroups.com
Can you show show example code please.

Alex Skinner

unread,
Jan 5, 2015, 4:55:21 PM1/5/15
to golan...@googlegroups.com
Use RequestPty on the session first.  There is an example of how to do this in the godoc.  Otherwise the sighup is never sent to the process.

Alex

Dave Cheney

unread,
Jan 5, 2015, 5:33:25 PM1/5/15
to golan...@googlegroups.com
Thanks for the suggestion Alex. I would have though that calling close on a Session would always close it regardless of the state of the remote end.


I'd like to see how the OP is using the ssh package and see I'd it could be improved.

Alex Skinner

unread,
Jan 5, 2015, 5:54:40 PM1/5/15
to golan...@googlegroups.com
I'm not sure how it could be easily solved.   OpenSSH client itself has the same behavior - ssh user@server "tail -f somefile"  never returns until tail is killed on the server.  Ctrl C here doesn't kill tail even though the session is gone.  However, once anything actually happens to whatever is being tailed, and tail needs to write, it will die since it doesn't have anything to write to anymore.  Same behavior in Go, which is why it finally returns(an error) after the session is closed and he manually writes to the file.

It wouldn't cause any issue on the client side, just having possibly forever waiting processes on the server side. 

Thanks,
Alex

Dave Cheney

unread,
Jan 5, 2015, 6:06:35 PM1/5/15
to golan...@googlegroups.com
That doesn't sound right, the remote tail is owned by the child sshd, certainly closing the session might leave a process running, but closing the connection should recover that.

In either case, I believe the go ssh client should be able to terminate its side of the session.

Jonas Riedel

unread,
Jan 6, 2015, 4:31:02 AM1/6/15
to golan...@googlegroups.com
Thanks Alex, your hint with RequestPty was right, now the Run terminates when the session gets closed.

Thanks a lot.

Jonas

Reply all
Reply to author
Forward
0 new messages