views:

82

answers:

3

Hi, I managed to install a Python script as a service using this recipe at ActiveState: win-services-helper.

In order to get much use out of it, I included the business end of my program by replacing lines 99 - 100:

 95   class Test(Service):
 96       def start(self):
 97           self.runflag=True
 98           while self.runflag:
 99               # My logging script
100               # goes here
101        def stop(self):
102            self.runflag=False
103            self.log("I'm done")
105
106    instart(Test, 'aTest', 'Python Service Test')

My problem is, the service logs only one entry and then stops by itself. Then I go to services.msc, attempt to start the service again, then Windows displays this message:

The Python Service Test service on Local Computer started and then stopped. Some services stop automatically when they have no work to do, for example, the Performance Logs and Alerts service.

How come my service thinks it has nothing to do when I placed it in an infinite loop? It should stop only when I shut down the computer and start working again the next time the computer is turned on, even without anyone logging in.

NOTE: My logging script uses simple file read/write to csv. It doesn't use any fancy logging modules.

+2  A: 

I believe that this message appears too when an error is encountered. It's not unlikely that the user running the service hasn't got the rights to write in the log file. You might try to create that csv file and set up the access rights so that "Everyone" can write to it.

steinar
Yes, I have unrestricted access to the folder in which I would create and write to my `csv`
Kit
Are you sure that the process running the service has access to that folder? Your user might not be the one running the service.
steinar
A: 

You need to see on the entire code when you determine the service stop, and you can user other tools like python daemonize to control your codes.

Primehaxor
A: 

It works now. Thanks everyone for the insights!

The recipe consisted of two scripts:

  • winservice.py
  • winservice_test.py

In my previous attempt, I picked out some lines which I thought were useful. I might have missed some of them, hence it didn't work.

I left winservice.py as it is. For the business end of my script, I placed it in a loop structure in winservice_test.py.

After writing the code, I went to the command line, browsed to where the two scripts were stored, then installed the service with

python winservice_test.py

My service can now be accessed through services.msc. By default, it runs as Local system account, which presents problems upon logging out. The service will still continue, but it will grind to a halt because the account doesn't have write access to the log folder.

I modified its Properties so that it runs with my user account (which has write access to the log folder). My service now survives logouts, stops working only upon shutdown, and also starts itself as soon as Windows boots up even without anyone logging in.

Kit