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()
8
  • Issue is when i run different function2 which has diff inputs ($PATH), subprocess variable2 is returning same value as in function1 Commented Jun 3, 2020 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? Commented Jun 3, 2020 at 12:00
  • Does this answer your question? Python subprocess/Popen with a modified environment Commented Jun 3, 2020 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. Commented Jun 3, 2020 at 12:51
  • 1
    Thanks MisterMiyagi...i get your point. Will check the suggestion and get back to you. Commented Jun 3, 2020 at 13:18

1 Answer 1

2

subprocess.terminate() should kill it

Sign up to request clarification or add additional context in comments.

3 Comments

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'
This is because it should be getOpatch.terminate(). It is the Popen instance that has the terminate method, not subprocess directly.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.