简体   繁体   中英

Windows service written in Python works only in debug mode

I've written a simple Windows service which should use websockets to report status of VirtualBox machines.

After installing and starting the service, my websocket server receives a connection request but the connection gets closed almost instantly.

server output when I start the service

running on port 8888
new connection
connection closed

Running the service with pythonservice.exe -debug myservice opens a websocket connection and sends the data I expect.

Server output when I start the service with debug flag

running on port 8888
new connection
message received VM NAME: win1, Memory: 111, CPUS: 1
message received VM NAME: win2, Memory: 266, CPUS: 1
message received VM NAME: win3, Memory: 256, CPUS: 1
message received VM NAME: lin1, Memory: 256, CPUS: 1
message received VM NAME: lin2, Memory: 200, CPUS: 1
message received VM NAME: lin3, Memory: 222, CPUS: 1
connection closed

Service source:

import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import time
import logging
import virtualbox
from websocket import create_connection

ws = create_connection("ws://192.168.56.1:8888/ws")

class VMInventory(win32serviceutil.ServiceFramework):
    _svc_name_ = "VMInventory"
    _svc_display_name_ = "VMInventory service"

    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.stop_event = win32event.CreateEvent(None,0,0,None)
        socket.setdefaulttimeout(60)
        self.stop_requested = False

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.stop_event)
        self.stop_requested = True

    def SvcDoRun(self):
        servicemanager.LogMsg(
            servicemanager.EVENTLOG_INFORMATION_TYPE,
            servicemanager.PYS_SERVICE_STARTED,
            (self._svc_name_,'')
        )
        self.main()

    def main(self):
        # Simulate a main loop
        vb = virtualbox.VirtualBox()
        while True:
            vms = vb.machines
            if self.stop_requested:
                break
            for vm in vms:
                ws.send("VM NAME: %s, Memory: %s, CPUS: %s" % (vm.name, str(vm.memory_size), str(vm.cpu_count)))
            time.sleep(5)
        ws.close()
        return

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(HelloWorldSvc)

Service itself is working just fine, my installation method was wrong. The correct way to install it is

python aservice.py  --username <username> --password <PASSWORD> --startup auto install

where <username> is prefixed with .\\ if using a local account, or with DOMAIN\\ if using a domain account.

For example

python aservice.py  --username .\johndoe --password mYstr0ngp4$$ --startup auto install

You can also just run the commands from and elevated prompt, so right click and "Run-As_Administrator". Unless you have to run under a specific user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM