1

I have a python code where i am using subprocess to get output of command "which opatch" in a function which gets output as unix path to getOpatch. Once that is done, how do I close the subprocess opened using Popen. Below is the code i use for calling subprocess. Thanks in advance.

import os
import subprocess

os.environ["ORACLE_HOME"] = X
os.environ["PATH"] += os.pathsep + os.pathsep.join([X + "/OPatch"])
getOpatch = subprocess.Popen("which opatch", shell=True, stdout=subprocess.PIPE).stdout
op_path = getOpatch.read()
Edison
  • 870
  • 1
  • 13
  • 28
Varma403
  • 11
  • 1
  • 5
  • Issue is when i run different function2 which has diff inputs ($PATH), subprocess variable2 is returning same value as in function1 – Varma403 Jun 03 '20 at 11:58
  • Please clarify your question. The subprocess automatically closes when done, there is no need to do so manually. Is your problem that your changes to ``os.environ`` persist? – MisterMiyagi Jun 03 '20 at 12:00
  • Does this answer your question? [Python subprocess/Popen with a modified environment](https://stackoverflow.com/questions/2231227/python-subprocess-popen-with-a-modified-environment) – MisterMiyagi Jun 03 '20 at 12:01
  • os.environ value changes when passing new value everytime but subprocess.Popen when fetching output of "which opatch" not changing to the latest. – Varma403 Jun 03 '20 at 12:51
  • You are *appending* to ``PATH`` every time the function is called, and never remove the added sub-path. Check the linked question on how to use an environment only for a single ``subprocess``. – MisterMiyagi Jun 03 '20 at 12:58
  • 1
    Thanks MisterMiyagi...i get your point. Will check the suggestion and get back to you. – Varma403 Jun 03 '20 at 13:18
  • I am unable to get it from the link you provided. Can you able to give me example how to remove the path variable that is added using os.environ. – Varma403 Jun 03 '20 at 13:45
  • You should use a context manager for this: from docs.python.org/3/library/subprocess.html : "Popen objects are supported as context managers via the with statement: on exit, standard file descriptors are closed, and the process is waited for. `with Popen(["ifconfig"], stdout=PIPE) as proc: log.write(proc.stdout.read())`" – Brian Minton Jun 03 '20 at 17:38

1 Answers1

2

subprocess.terminate() should kill it

lolsu
  • 53
  • 8
  • i am getting below error when using terminate. File "/z01/EBS_PATCH_TOOL/modules_seq.py", line 106, in OHome_1012 subprocess.terminate() AttributeError: 'module' object has no attribute 'terminate' – Varma403 Jun 03 '20 at 12:52
  • 2
    This is because it should be `getOpatch.terminate()`. It is the `Popen` instance that has the `terminate` method, not `subprocess` directly. – alani Jun 03 '20 at 16:50
  • 1
    In any case, the command that is launched in this example (essentially `sh -c 'which opatch'`) will exit immediately after printing a line of output, so there is no good reason to try to terminate it. – alani Jun 03 '20 at 16:55