简体   繁体   English

进程完成后杀死 subprocess.Popen().write

[英]kill subprocess.Popen().write after the process is finished

I am trying to update the router with a python script with only one ssh call.我正在尝试使用只有一个 ssh 调用的 python 脚本来更新路由器。 However, the kill() function is executed before the update starts.但是,kill() 函数在更新开始之前执行。

process_1 = f' opkg update'
process_2 = f' echo process 2'

cmds = [
    f'{process_1}\n',
    f'{process_2}'
]

proc = subprocess.Popen(["ssh", "root@192.168.1.1"], stdin=subprocess.PIPE)

for cmd in cmds:
    proc.stdin.write(f'{cmd}'.encode())

proc.stdin.flush()
proc.stdin.close()
proc.kill()

Solution解决方案

.wait() is the method I was looking for .wait() 是我正在寻找的方法

process_1 = f' opkg update'
process_2 = f' echo process 2'

cmds = [
    f'{process_1}\n',
    f'{process_2}'
]

proc = subprocess.Popen(["ssh", "root@192.168.1.1"], stdin=subprocess.PIPE)

for cmd in cmds:
    proc.stdin.write(f'{cmd}'.encode())

proc.stdin.flush()
proc.stdin.close()
proc.wait()

Passing commands to standard input of ssh is somewhat finicky.将命令传递给ssh的标准输入有点挑剔。 A much better solution is to switch to Paramiko, but if your needs are simple, just refactor to pass the commands as arguments to ssh .一个更好的解决方案是切换到 Paramiko,但如果您的需求很简单,只需重构以将命令作为参数传递给ssh

result = subprocess.run(
    ["ssh", "root@192.168.1.1",
     "\n".join(cmds)],
    check=True)

Like the documentation already tells you, you should generally prefer subprocess.run (or its legacy siblings check_call , check_output , etc) over Popen whenever you can.就像文档已经告诉您的那样,您通常应该尽可能喜欢subprocess.run (或它的旧兄弟check_callcheck_output等)而不是Popen The problems you were experiencing is one of the symptoms, and of course, the fixed code is also much shorter and easier to understand.您遇到的问题是症状之一,当然,固定代码也更短且更易于理解。

As a further aside, f'{thing}' is just a really clumsy way to write thing (or str(thing) if thing isn't already a string).另外, f'{thing}'只是一种非常笨拙的编写方式(如果thing还不是字符串thing则为str(thing) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在进程执行开始之后(在完成之前)向 subprocess.Popen() 提供 stdin 输入 - Supply stdin input to subprocess.Popen() after execution of the process had begun ( and before it had finished ) 杀死通过子进程在后台启动的SimpleHTTPServer进程 - Kill SimpleHTTPServer process which is started in background with subprocess.Popen 在 Windows 中轻轻杀死使用 Python 的 subprocess.Popen() 创建的进程 - Gently kill a process created with Python's subprocess.Popen() in Windows 如何杀死在 Windows 中使用 subprocess.pOpen() 打开的这个 python 进程? - How to kill this python process opened using subprocess.pOpen() in Windows? 使用subprocess.Popen恢复进程? - Recover process with subprocess.Popen? 使用subprocess.Popen()后关闭process.stdout - closing process.stdout after using subprocess.Popen() 杀死在python中用subprocess.popen打开的adb - kill adb opened with subprocess.popen in python 如何终止使用Subprocess.Popen启动进程并移至下一行代码的函数? - How to kill a function which starts a process using Subprocess.Popen and move to next line of code? 如何杀死 subprocess.Popen() 进程产生的子进程? - How can I kill child processes spawned by the subprocess.Popen() process? Subprocess.Popen参数不传递给新进程 - Subprocess.Popen argument not passing to new process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM