Executing Commands From Windows Service

Stephen Hansen apt.shansen at gmail.com
Sun Feb 7 20:17:40 CET 2010


On Sun, Feb 7, 2010 at 11:02 AM, T <misceverything at gmail.com> wrote:

> I have a script, which runs as a Windows service under the LocalSystem
> account, that I wish to have execute some commands.  Specifically, the
> program will call plink.exe to create a reverse SSH tunnel.  Right now
> I'm using subprocess.Popen to do so.  When I run it interactively via
> an admin account, all is well.  However, when I'm running it via
> service, no luck.  I'm assuming this is to do with the fact that it's
> trying to run under the LocalSystem account, which is failing.  What
> would be the best way around this?  Thanks!
>

I don't know what your specific issue is, but here's some tips for running
Python as a service on windows which are not always immediately obvious and
can cause failures:

- The search path is screwy: if you are importing a module that happens to
be the same name as a dll in system32 (even if this isn't at all a python
dll), it can get confused.
- There is *no* sys.stdout! This is a big one. If any piece of code you're
using ever does 'print', the whole thing can crash hard. I replace
sys.stdout and sys.stderr with a fake file-like object that catches errors
in attempting to .write to the real one and ignores them.

If neither of those two are a problem for you, you need to define "no luck"
before anyone will be able to help you. Are there errors in the event
viewer? Are you getting an exception that's killing out your service
(capture and write to a file with the logging module)? Or is the Popen call
being run and returning but just not doing anything (in which case, try
capturing output from the command and see if it indicates an error message
from plink.exe). Etc.

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100207/c7f845dd/attachment.html>


More information about the Python-list mailing list