简体   繁体   中英

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

I am executing the following command in a python script:

#! /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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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