【发布时间】:2015-10-06 05:10:54
【问题描述】:
我使用Paramiko 的exec_command 如下在服务器上运行后台命令:
client.exec_command('test > /dev/null 2 > &1 &')
但我看不到'ps aux | grep test'的过程。
为什么会这样?
【问题讨论】:
我使用Paramiko 的exec_command 如下在服务器上运行后台命令:
client.exec_command('test > /dev/null 2 > &1 &')
但我看不到'ps aux | grep test'的过程。
为什么会这样?
【问题讨论】:
因为“test”命令以给定表达式确定的状态退出。
命令在后台运行,但马上退出。
使用另一个命令,您会看到它在后台运行。
罗伯
【讨论】:
sleep 100。另外,请检查nohup。
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
【讨论】: