capture stdout and stderror from within a Windows Service?

C

Chris Curvey

I'm trying to get this invocation right, and it is escaping me. How
can I capture the stdout and stderr if I launch a subprocess using
subprocess.check_call()? The twist here is that the call is running
from within a Windows service.

I've tried:

check_call("mycmd.exe", stdout=subprocess.PIPE) [raises an exception
"An integer is required"]

check_call("mycmd.exe", stdout=file("c:\\temp\\foobar.txt", "w"))
[raises an exception "An integer is required"]
 
C

Chris Rebert

I'm trying to get this invocation right, and it is escaping me.  How
can I capture the stdout and stderr if I launch a subprocess using
subprocess.check_call()?  The twist here is that the call is running
from within a Windows service.

I've tried:

check_call("mycmd.exe", stdout=subprocess.PIPE)  [raises an exception
"An integer is required"]

check_call("mycmd.exe", stdout=file("c:\\temp\\foobar.txt", "w"))
[raises an exception "An integer is required"]

Providing the full exception Tracebacks would be highly recommended.

Cheers,
Chris
 
N

norseman

Chris said:
I'm trying to get this invocation right, and it is escaping me. How
can I capture the stdout and stderr if I launch a subprocess using
subprocess.check_call()? The twist here is that the call is running
from within a Windows service.

I've tried:

check_call("mycmd.exe", stdout=subprocess.PIPE) [raises an exception
"An integer is required"]

mycmd.exe isn't open (running). the mycmd.exe is supposed to be a fn.
How did you start it?
check_call("mycmd.exe", stdout=file("c:\\temp\\foobar.txt", "w"))
[raises an exception "An integer is required"]
==============
I think you need to run the .exe from your .py using the subprocess
module's commands and hook in at that time to stdin, stdout, ....
get the fn and use check_call() to see if .exe has something to say.

Check subprocess's help/man and so forth.

I know that if you start a program via os.Popen2's popen3 things work as
expected. How you would redirect I/O in an arbitrary already running
program in Windows is a good question.


Steve
 
D

David Lyon

I'm trying to get this invocation right, and it is escaping me. How
can I capture the stdout and stderr if I launch a subprocess using
subprocess.check_call()? The twist here is that the call is running
from within a Windows service.

I've tried:

check_call("mycmd.exe", stdout=subprocess.PIPE) [raises an exception
"An integer is required"]

check_call("mycmd.exe", stdout=file("c:\\temp\\foobar.txt", "w"))
[raises an exception "An integer is required"]

In the past, windows services have never been allowed to make any output
to the console or the GDI or get any user input. Rules said they could
send stuff to syslog etc.

All writing can go to logfiles and so forth...

I've never heard of that being allowable....

David
 
C

Chris Curvey

I'm trying to get this invocation right, and it is escaping me.  How
can I capture the stdout and stderr if I launch a subprocess using subprocess.check_call()?  The twist here is that the call is running
from within a Windows service.

I've tried:

check_call("mycmd.exe", stdout=subprocess.PIPE)  [raises an exception
"An integer is required"]

check_call("mycmd.exe", stdout=file("c:\\temp\\foobar.txt", "w"))
[raises an exception "An integer is required"]

Ahhh, Blake put me on the right track. If you want any of the
streams, you have to supply values for all of them, like so:

p = subprocess.Popen(step, shell=True
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)

(stdout, stderr) = p.communicate()

and, incidentally, it appears that you have to use UNC paths in your
Popen call -- drive letters won't work.

so "dir \\foobar\myshare\mydir" will work, but "dir j:\mydir" will
not.

Many thanks for all your assistance.
 
T

Tim Golden

Chris said:
Ahhh, Blake put me on the right track. If you want any of the
streams, you have to supply values for all of them, like so:

p = subprocess.Popen(step, shell=True
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)

(stdout, stderr) = p.communicate()

and, incidentally, it appears that you have to use UNC paths in your
Popen call -- drive letters won't work.


This is because you're running in a service. Drive letters
aren't system wide: they're per-user. The service won't
know about any drive letters you or anyone else happens
to have mapped.

TJG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top