【问题标题】:Can I run a command in background on server by exec_command of paramiko?我可以通过 paramiko 的 exec_command 在服务器后台运行命令吗?
【发布时间】:2015-10-06 05:10:54
【问题描述】:

我使用Paramikoexec_command 如下在服务器上运行后台命令

client.exec_command('test > /dev/null 2 > &1 &')

但我看不到'ps aux | grep test'的过程。

为什么会这样?

【问题讨论】:

    标签: python linux ssh paramiko


    【解决方案1】:

    因为“test”命令以给定表达式确定的状态退出。

    命令在后台运行,但马上退出。

    使用另一个命令,您会看到它在后台运行。

    罗伯

    【讨论】:

    • 例如使用sleep 100。另外,请检查nohup
    【解决方案2】:

    paramiko 为每个 .exec_command() 生成一个线程,因此您不必像在 bash 中一样添加通常的 shell 魔术 &.exec_command() 将立即返回,您必须注意读取其缓冲区(stdin、stdout、sterr)

    client.exec_command('test > /dev/null 2 > &1')
    time.sleep(5)
    client.exec_command('killall -9 test')
    ...
    # the remot command will be force killed if you close the channel. (may depend on sshd implementation)
    

    这将在一个单独的线程中运行test 将stderr 重定向到stdout。然后主线程将休眠 5 秒,并产生另一个线程来杀死测试,或者如果你不杀死它,你只需关闭通道,远程 sshd 将处理你会话期间产生的所有 procs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-18
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多