Use Python to create Windows and python Services

Source: Internet
Author: User

Use Python to create Windows and python Services

Recently, a Python program needs to be installed and run as a Windows system service. Some pitfalls have been encountered during the process.

Python service class

First, the Python program must call some Windows system APIs to serve as a system service. The specific content is as follows:

#!/usr/bin/env python# -*- coding: utf-8 -*-import sysimport timeimport win32apiimport win32eventimport win32serviceimport win32serviceutilimport servicemanagerclass MyService(win32serviceutil.ServiceFramework):  _svc_name_ = "MyService"  _svc_display_name_ = "My Service"  _svc_description_ = "My Service"  def __init__(self, args):    self.log('init')    win32serviceutil.ServiceFramework.__init__(self, args)    self.stop_event = win32event.CreateEvent(None, 0, 0, None)  def SvcDoRun(self):    self.ReportServiceStatus(win32service.SERVICE_START_PENDING)    try:      self.ReportServiceStatus(win32service.SERVICE_RUNNING)      self.log('start')      self.start()      self.log('wait')      win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)      self.log('done')    except BaseException as e:      self.log('Exception : %s' % e)      self.SvcStop()  def SvcStop(self):    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)    self.log('stopping')    self.stop()    self.log('stopped')    win32event.SetEvent(self.stop_event)    self.ReportServiceStatus(win32service.SERVICE_STOPPED)  def start(self):    time.sleep(10000)  def stop(self):    pass  def log(self, msg):    servicemanager.LogInfoMsg(str(msg))  def sleep(self, minute):    win32api.Sleep((minute*1000), True)if __name__ == "__main__":  if len(sys.argv) == 1:    servicemanager.Initialize()    servicemanager.PrepareToHostSingle(MyService)    servicemanager.StartServiceCtrlDispatcher()  else:    win32serviceutil.HandleCommandLine(MyService)

Pyinstaller Packaging

pyinstaller -F MyService.py

Test

# Install the service dist \ MyService.exe install # start the service SC start MyService # stop the service SC stop MyService # delete the service SC delete MyService

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.