1

I want to run the following os.system command as a subprocess in order to know its pid since I want to be able to perform a

proc.terminate()

Bellow is the os.system() Command that works. Note that the bellow command already opens a subshell since thats what the setview is doing, and then executes a python script inside that subshell and then exits that subshell.

   os.system("/usr/atria/bin/cleartool setview -exec '/usr/bin/python /home/testUser/Development/Scripts/setDoneFlag_Count_Lines.py' testUser__project_5_0_myProject_001")

I tried

import subprocess
cmd = "/usr/atria/bin/cleartool setview -exec '/usr/bin/python /home/testUser/Development/Scripts/setDoneFlag_Count_Lines.py' testUser__project_5_0_myProject_001"
p=subprocess.Popen(cmd.split(), shell=True)

Taken from this stackoverflow thread: How to determine pid of process started via os.system But it only executes the first /usr/atria/bin/cleartool and not the other commands. Anyone knows how to write a subshell that is equivalent to my os.system call?

Thanks in advance.

Community
  • 1
  • 1
anders
  • 1,535
  • 5
  • 19
  • 43
  • please refer http://stackoverflow.com/questions/7989922/opening-a-process-with-popen-and-getting-the-pid – hardythe1 May 28 '15 at 10:59

2 Answers2

0

.split is not doing you any favors here, because you have spaces within quotes that you don't want to split on. Either pass a string directly, and let the shell handle argument separation:

cmd = "/usr/atria/bin/cleartool setview -exec '/usr/bin/python /home/testUser/Development/Scripts/setDoneFlag_Count_Lines.py' testUser__project_5_0_myProject_001"
p=subprocess.Popen(cmd, shell=True)

Or separate the command manually:

cmd = [
    "/usr/atria/bin/cleartool",
    "setview",
    "-exec",
    # single quotes in the argument no longer required, as we're not going through the shell
    "/usr/bin/python /home/testUser/Development/Scripts/setDoneFlag_Count_Lines.py",  
    "testUser__project_5_0_myProject_001"
]
p = subprocess.Popen(cmd)
Eric
  • 95,302
  • 53
  • 242
  • 374
  • @anders: `shlex.split()` could be used to get the list. You don't need `shell=True` here. – jfs May 30 '15 at 19:45
-1

Why are you splitting your command? Don't use cmd.split(). Try this:

import subprocess
cmd = "/usr/atria/bin/cleartool setview -exec '/usr/bin/python /home/testUser/Development/Scripts/setDoneFlag_Count_Lines.py' testUser__project_5_0_myProject_001"
p=subprocess.Popen([cmd], stdout=subprocess.PIPE,shell=True)
out, err = p.communicate()
print out
Bhoomika Sheth
  • 323
  • 5
  • 17
  • This will try to run the executable called `/usr/atria/bin/cleartool setview...`, ie all the arguments will be treated as part of the executable name – Eric May 28 '15 at 11:02