[python-win32] how to run python in background on Windows ?

Michael Li mli at deform.com
Fri Jun 4 13:25:10 EDT 2004


Hi, Jim:

Thank you very much.
My scripts are little bit longer, it's not easy to post here.
I just want to put this into background to run 24x7 :
python myscript.py

In Unix, it's very easy:
% python myscript.py &

In Windows, I wrote myinstall.cpp, myrun.cpp.
myrun.cpp is a small program just following the pattern of
Windows service program.
"myinstall" will install the "myrun.exe" service into Windows service
control manager. From Control Panel->Service, when I click
start button of myservice, it will start python.
I can see python is running from Task Manager.
When I log out and log in again, the python is gone.
I can see myrun.exe is still there from Task Manager.

I use Windows XP Home Edition.
Does this matter ?
I have a pure Windows program running as a service without
any problems.

Also I got two strange thing:
1. I have to use my login account to start python.
    After installation, I saw that "Log On" of my service
    is "Local System account". After reboot the machine,
    I can see "myrun.exe", but no python started.
    I have to change to my login account with login name/password
    in the properties of my service in Control panel.
    Then I reboot my machine, no python is running, I
    have a client program in another computer to check it.
    After I log into my account, I saw python is running.
    But my pure Windows program runs well without login.
    After login, I saw that the user name of my pure Windows program
    is "SYSTEM".

2. In my python scripts, I have to check whether a file exists or not.
    I use "nRetCode = os.access(file_name, F_OK)" to check it.
    When the file is in local harddisk, I don't have any problems.
    But when the file is in mapped harddisk, I got problem.
    Let's say that my file is C:\temp\t.txt.
    No matter I run python in DOS window or in service, it always
    return the correct value, that is, "nRetCode" is True.
    When the file is X:\temp\t.txt, where X is mapped drive.
    If I run python in DOS window, it returns the correct value.
    If I run python in service mode(background), it returns
    the wrong value, that is, "nRetCode" is False.
    I believe that this is related to Windows security check, but
    I dont know how to fix it. Do you have any idea ?

How do you run your python scripts in background ?

Best regards.
Michael Li


Jim Vickroy wrote:
> Hi Michael,
> 
> Could you post a short sample script that demonstrates the behavior.
> 
> I have several Python scripts running as MS Windows Services 24x7 on
> machines where no one is logged in and I have not seen this behavior.  What
> version of Windows are you using?
> 
> -- jv
> 
> -----Original Message-----
> From: python-win32-bounces at python.org
> [mailto:python-win32-bounces at python.org]On Behalf Of Michael Li
> Sent: Thursday, June 03, 2004 9:14 AM
> To: python-win32 at python.org
> Subject: Re: [python-win32] how to run python in background on Windows ?
> 
> 
> Hi, Tom
> Thank you very much for your reply.
> This is not a "automatically startup" issue.
> 
> I can start/stop python by using of Windows control
> program in the same login session, but when I logout
> and login again, the python service is gone. I have
> to go to control panel to start again.
> Do you know that is there a way to run python as
> a service ?
> 
> 
> Whittingham, Thomas wrote:
> 
>>This may be obvious but did you set up the service to start
>>automatically?  In the same dialog boxes that let you start/stop Windows
>>services you can specify whether a service should run at startup.
>>
>>~Tom
>>
>>Tom Whittingham
>>Data Analyst
>>Treatment Research Center
>>Whittingham_T at mail.trc.upenn.edu
>>215-222-3200 x143 (work)
>>215-386-5106 (fax @ work)
>>267-456-6802 (cell/home)
>>
>>"For You so loved the unlovable
>>That You gave the Ineffable
>>That who so believes the unbelievable
>>Will gain the unattainable."
>>
>>-- "There You Go" lyrics, by Caedmon's Call
>>
>>-----Original Message-----
>>From: Michael Li [mailto:mli at deform.com]
>>Sent: Thursday, June 03, 2004 10:19 AM
>>To: python-win32 at python.org
>>Subject: [python-win32] how to run python in background on Windows ?
>>
>>Hi,
>>
>>I was trying to run my python program as a server in background
>>on Windows, but I failed.
>>
>>On Unix, I can do :
>>% python myscript.py &
>>
>>On Windows, I use Windows' service to start/stop
>>the python. In the same login session, I got success,
>>I can see python program from TaskManager.
>>but when I log out and log in again, my python server
>>is gone, I can not see python program from TaskManager.
>>
>>Is there anyone there knowing how to run python
>>in background as Windows' service ?
>>
>>best regards.
>>Michael Li
>>
>>
>>_______________________________________________
>>Python-win32 mailing list
>>Python-win32 at python.org
>>http://mail.python.org/mailman/listinfo/python-win32
>>
>>_______________________________________________
>>Python-win32 mailing list
>>Python-win32 at python.org
>>http://mail.python.org/mailman/listinfo/python-win32
> 
> 
> 
> _______________________________________________
> Python-win32 mailing list
> Python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
> 




More information about the Python-win32 mailing list