Python script as Windows service + on schedule + logging

  • Testing on Windows 7, 2008, 2012

In line: timer = schedule.every(10).seconds, you sets schedule time.
In line:, where test1 — this your main script(program) to execute schedule task.
In line: _svc_name_ = «mytest-service» — this is your service name.
In line: _svc_display_name_ = «mytest-service» — this is your service display name in Windows service list.

Edit by yourself and save this code to file

import win32serviceutil  #
import win32service
import win32event
import servicemanager
import socket
import time
import logging
import logging.handlers
import schedule  #
from mytest import test1

# Managaement of service
# python.exe install | remove | start | stop | help

# Sets log file path.
log_file ="c:\\output.log";

# Return a logger with the specified name.
mylogger = logging.getLogger("MyLogger")

# Sets the threshold for this logger to lvl. Logging messages which are less severe than lvl will be ignored.

# Sets rotation parameters of disk log files
handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=10485760, backupCount=2)

# Sets format of record in log file
formatter = logging.Formatter('%(asctime)s - %(module)-10s - %(levelname)-8s %(message)s', '%d-%m-%Y %H:%M:%S')

# Adds the specified handler to logger "MyLogger"

class WinService(win32serviceutil.ServiceFramework):
    _svc_name_ = "mytest-service"    # service name
    _svc_display_name_ = "mytest service"    # display service name

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

    # stop command service
    def SvcStop(self):
        win32event.SetEvent(self.stop_event)"*** STOPPING SERVICE ***\n")
        self.stop_requested = True

    # start command service
    def SvcDoRun(self):
            (self._svc_name_, '')
        )"*** STARTING SERVICE ***\n")
        # run main process

    # main process
    def main(self):"... STARTING SCHEDULE PROCESS ...\n")
        # schedule time for our task(program)
        # For example, I use "test1" procedure from "mytest" own module
        timer = schedule.every(10).seconds
        # run main loop for schedule process while service runs
        while not self.stop_requested:
            # execute task on schedule

if __name__ == '__main__':

For example, I use simple procedure «test1» from file

import logging

# Return a logger with the specified name.
mylogger = logging.getLogger("MyLogger")

def test1():"Run!\n")

Create Windows service which will run procedure «test1». For this you should call command line (CMD) and run this command:

python.exe install


Next step, start this service:

python.exe start

— Or start service from Windows service list

After some time try stopping service and see log file:

python.exe stop

— Or stop service from Windows service list

In log file you can see following info:
19-07-2016 10:34:20 — serv       — INFO     *** STARTING SERVICE ***

19-07-2016 10:34:20 — serv       — INFO     … STARTING SCHEDULE PROCESS …

19-07-2016 10:34:30 — mytest     — INFO     Run!

19-07-2016 10:34:40 — mytest     — INFO     Run!

19-07-2016 10:34:50 — mytest     — INFO     Run!

19-07-2016 10:35:00 — mytest     — INFO     Run!

19-07-2016 10:35:10 — mytest     — INFO     Run!

19-07-2016 10:35:14 — serv       — INFO     *** STOPPING SERVICE ***

P.s.: Have a good day!