Paramiko and exec_command - killing remote process?

前端 未结 8 1607
情书的邮戳
情书的邮戳 2020-12-09 05:09

I\'m using Paramiko to tail -f a file on a remote server.

Previously, we were running this via ssh -t, but that proved flaky, and the

相关标签:
8条回答
  • 2020-12-09 05:36

    You can use get_pty as described in https://stackoverflow.com/a/38883662/565212.

    E.g. scenario - When to call client/channel.close():
    Step1: Execute a remote command that writes to the log file.
    Step2: Spawn a thread that executes the tail command and blocks in the readline loop
    Step3: In main thread, when the command returns, you know there will be no more logs, kill the tail thread.

    0 讨论(0)
  • 2020-12-09 05:42

    I just hit this issue and wasn't in a position to issue a pkill to close the process at the end.

    A better solution is to change the command you are running to:

    tail -f /path/to/file & { read ; kill %1; }
    

    This will let you run your tail command for as long as you need. As soon as you send a newline to the remote process the kill %1 will execute and stop the tail command you backgrounded. (for reference: %1 is a jobspec and used to describe the first process that has been backgrounded in your session, ie the tail command)

    0 讨论(0)
提交回复
热议问题