nish
nish

Reputation: 7280

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

Upvotes: 0

Views: 884

Answers (1)

Thomas Orozco
Thomas Orozco

Reputation: 55293

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.

Upvotes: 1

Related Questions