简体   繁体   English

Python,如何在执行后终止“ shell = True”子进程

[英]Python, How to terminate a “shell=True” subprocess after execution

I am executing the following command in a python script: 我正在python脚本中执行以下命令:

#! /usr/bin/python

import shlex
import os
import subprocess
import string
import random
import signal



pro = subprocess.Popen("zcat production_dump_2013-09-16_12-00.sql.gz | PGPASSWORD=everything psql -d voylla_solr -h localhost -p 5432 -U nishant", shell=True)
pro.wait()
os.kill(pro.pid, signal.SIGTERM)

This gives me: 这给了我:

OSError: [Errno 3] No such process

I also tried using 我也尝试使用

pro = subprocess.Popen("zcat production_dump_2013-09-16_12-00.sql.gz | PGPASSWORD=everything psql -d voylla_solr -h localhost -p 5432 -U nishant", shell=True)
pro.wait()
pro.kill()

and this gives me: 这给了我:

OSError: [Errno 3] No such process

How do I kill the process after execution to execute next commands 如何在执行后杀死进程以执行下一个命令

Popen.wait waits for the process to terminate, so there is nothing left to kill once it returns. Popen.wait等待进程终止,因此一旦返回,将Popen.wait杀死。

Once a process exits, you're not supposed to "kill it". 一旦进程退出,您就不应“杀死它”。 The only thing you're supposed to do is collect its return code, which Popen.wait does for you. 您唯一要做的就是收集其返回代码, Popen.wait会为您执行此操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM