Hi,

I created a python script and trying to run it as a windows service. In my script I am reading some data from a configuration file . My problem is , if I run the script in python editor it will work fine and when running the same as windows service it throws an error saying that there is no such section in the file

The code that reads the configuration file is as follows,

cp = ConfigParser()
cp.read ("feedmonitor.cfg")
self._server = cp.get ("mail", "server")

and the configuration file is feedmonitor.cfg and its contents are as follows,

[mail]
server=192.168.0.5
port=
user=
password=

[monitor]
file = D:\pyscripts\txn_%Y%m%d.log
start = 07:30:00
end = 18:00:00
timeout = 180

Error is as follows,

File "d:\pyscripts\FeedMonitor1.py", line 32, in SvcDoRun
self.loadConfig()
File "d:\pyscripts\FeedMonitor1.py", line 60, in loadConfig
print cp.get("mail","server")
File "D:\Python25\Lib\ConfigParser.py", line 511, in get
raise NoSectionError(section)
<class 'ConfigParser.NoSectionError'>: No section: 'mail'


I wonders how the same code behaves differently in these cases.

Any solution on the above will be appreciated,

Thanks,
Sreejith

cp.read ("feedmonitor.cfg") When running as a Windows service you probably have a different default directory. It may not be finding the configuration file you expect.

Thanks BearofNH.

Thanks a lot. When I gave the full path, it is working fine.

Another problem with me is I am able to start my script as windows service. But once I stopped the service I can't restart it or re-install the service. After stoping the service it shows the message that the service "ServiceName" is stopped and if Itry to start it again, it shows a message that

'Error starting service: An instance of the service is already running'

After that if I removed the service , then too no problem but if I install it again , then it shows the message

'Error installing the service: The specified service has been marked for deletion'

Can you say why this happening? And is there any solution to stop and restart the service??

Any help would be greatly appreciated.

Thanks,
Sreejith.

You say you "start my script as windows service". Exactly what does that involve? You don't just take a program and suddenly have it run as a service. But I get the feeling that's what you're trying to do.

There's a pile of programming that goes into making Windows services work, starting with OpenSCManager() and going on and on. You have to write handlers for all the possible control functions (stop, pause restart, unload ...). That's how you tell Windows your service is stopped, so it doesn't think 'An instance of the service is already running'.

In addition to the base Windows functions -- which you can Google -- there are various libraries that help do this for you, but I don't know of any that are Python-based.

This article has been dead for over six months. Start a new discussion instead.