Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging a subprocess.Popen call

I have been using subprocess.Popen successfully in the past, when wrapping binaries with a python script to format arguments / customize etc...

Developing a nth wrapper, I did as usual... but nothing happens.

Here is the little code:

print command
p = subprocess.Popen(command, shell = True)
result = p.communicate()[0]
print vars(p)
return result

And here is the output:

/usr/bin/sh /tmp/run/launch.sh
{'_child_created': True, 'returncode': 0, 'stdout': None, 'stdin': None, 'pid': 21650, 'stderr': None, 'universal_newlines': False}

As you can see, the goal is to create a shell script setting up everything I need, and then executing it. I would prefer to use real python code, but unfortunately launch.sh call 3rd party shell scripts that I have no wish to try and replicate (though I've been insisting for a python api for over a year now).

The problem is that:

  • the shell script is not executed (it should spawn process and output some little things)
  • no python exception is raised
  • there is nothing in the p object that indicates that an error occurred

I have tried check_call without any success either...

I am at a loss regarding what I should do, and would be very glad if someone could either point my mistake or direct me toward resolution...

EDIT:

  • Trying to run this on Linux (sh)
  • shell is necessary for variable substitution in the scripts invoked

EDIT 2:

Following badp suggestion, I tweaked the code and added

subprocess.Popen('ps', shell = True).communicate()

Right after p = ... line that creates the process, here is the output:

/usr/bin/sh /tmp/run/launch.sh
  PID TTY          TIME CMD
29978 pts/0    00:00:01 zsh
 1178 pts/0    00:00:01 python
 1180 pts/0    00:00:00 sh <defunct>
 1181 pts/0    00:00:00 ps
None

Apparently the process is launched (even though <defunct>) and one should also note that I have a little problem passing the parameters in...

Thanks.

like image 979
Matthieu M. Avatar asked Feb 05 '10 10:02

Matthieu M.


People also ask

What is the difference between subprocess call and Popen?

Popen is more general than subprocess. call . Popen doesn't block, allowing you to interact with the process while it's running, or continue with other things in your Python program. The call to Popen returns a Popen object.

What is Popen in subprocess?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.

Does subprocess Popen block?

Popen is nonblocking. call and check_call are blocking. You can make the Popen instance block by calling its wait or communicate method.

Does subprocess Popen need to be closed?

There the connection is not closed. So you do not need to close most probably. unrelated: you could use stdin=open('test.


2 Answers

I've finally found the answer to my question, thanks to badp and his suggestions for debugging.

From the python page on the subprocess module:

The executable argument specifies the program to execute. It is very seldom needed: Usually, the program to execute is defined by the args argument. If shell=True, the executable argument specifies which shell to use. On Unix, the default shell is /bin/sh. On Windows, the default shell is specified by the COMSPEC environment variable. The only reason you would need to specify shell=True on Windows is where the command you wish to execute is actually built in to the shell, eg dir, copy. You don’t need shell=True to run a batch file, nor to run a console-based executable.

Since I am on Linux and using shell=True, my command is in fact a list of arguments to be executed by executable, which defaults to /bin/sh. Thus the full command executed was: /bin/sh /usr/bin/sh /tmp/run/launch.sh... which did not work so well.

And I should have used either:

subprocess.Popen('/tmp/run/launch.sh', shell=True)

or

subprocess.Popen('/tmp/run/launch.sh', executable = '/usr/bin/sh', shell=True)

It's tricky that shell=True would actually modify the default executable value on Linux only...

like image 73
Matthieu M. Avatar answered Oct 23 '22 07:10

Matthieu M.


Try this:

p = subprocess.Popen(command,
                     shell = True, #is this even needed?
                     stdin = subprocess.PIPE,
                     stdout = subprocess.PIPE,
                   # stderr = subprocess.STDOUT #uncomment if reqd
                    )

Tested working on Windows with the ping command. This lets you communicate, which might help you find out why the script isn't launched in the first place :)

like image 27
badp Avatar answered Oct 23 '22 09:10

badp