0

I have been asked to run a python script from the Windows Task Scheduler.

In my python script I will substantially use watchdog library in order to instanciate and run an Observer that will monitor file changes in a specific directory path

file_handlers.py

import time
import logging

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

from elaboration_directory import FILE_PROCESS_DIR

logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


class FileHandler(FileSystemEventHandler):

    def on_created(self, event):
        if not event.is_directory:
            file_path = event.src_path
            logger.info('File added. File path: "%s"' % file_path)


class ElaborationFolderWatcher(object):

    def start(self):
        event_handler = FileHandler()
        observer = Observer()
        observer.schedule(event_handler, path=FILE_PROCESS_DIR, recursive=False)
        observer.start()
        logger.info('Observer: started')

        try:
            while True:
                # keep observer active
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join()


if __name__ == '__main__':
    logger.info('Python script started')
    elaboration_folder_watcher = ElaborationFolderWatcher()
    elaboration_folder_watcher.start()

My issue is that when I run the script from the Task Scheduler a terminal will open, and if I close it it will kill the process (and Observers).

Question

How can I run the python script from Windows Task Scheduler without opening any window?

Solution

Possible solution: instead of directly running the file_handlers.py I can run a .bat script that will call the file_handlers.py. By adding the argument pythonw.exe to the start command, no window will be opened.

run_program.bat (Target this file with Task Scheduler action)

@echo off

::Identify the path of directory containing python script using relative path

set SCRIPT_DIR=%~dp0
set PYTHON_SCRIPT=%SCRIPT_DIR%\file_handlers.py

:: Run the Python script. Argument "pythonw.exe" will avoid cmd opening

start "" "pythonw.exe" "%PYTHON_SCRIPT%" %*
3
  • 1
    Why don't you run without a terminal in the first place? Commented Jul 24, 2024 at 14:44
  • Certainly do not need a batch file to run a python script. Commented Jul 24, 2024 at 14:51
  • @tripleee I've been asked to keep the python script runnable from the "windows task scheduler" (It probably was worth mention, I will edit the question soon). I've just realized that I can simply run an action that runs "file_handlers.py" and it will work perfectly but a terminal will still open and as soon I close it the execution will be stopped. Commented Jul 24, 2024 at 14:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.