Search code examples
pythonloggingwindows-server-2012

Python logger not logging when called with scheduler on windows server 2012


I wrote a simple "git pull" script to be run on a Windows Server:

import logging
import subprocess

# Initialize logging
logger = logging.getLogger('autopull')
logger.setLevel(logging.INFO)
log_file = logging.FileHandler('autopull.log')
log_file.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s:%(levelname)s:%(message)s")
log_file.setFormatter(formatter)
logger.addHandler(log_file)

# Main part
logger.info('Started')
try:
    subprocess.check_output("C:\Program Files\Git\cmd\git pull")
    logger.info('Pulled repo successfully')
except:
    logger.exception("Pulled failed")

I used a logger because the scheduler always told me that it executes with exit code 0 and I wanted to double check whether it was the case.

When I call the script by hand, it is working fine and the logger writes in the autopull.log. However, when it is called by the scheduler, the logger writes nothing. The task runs with the "Run with highest privileges" and "Run whether user is logged or not".

Is there something I did wrong ?

Edit 1 : Added Scheduled Task parameters

General :

  • "Run whether user is logged on or not" and "Run with highest privileges".
  • Configure for : Windows Server 2012 R2

Triggers:

  • Trigger : One time
  • Details : At 12:30 PM on 6/26/2017 - After triggered, repeat every 1 hour indefinitely
  • Status : Enabled

Actions:

  • Action : Start a program
  • Details : D:\Anaconda3\python.exe D:\projects\foo\autopull.py

Conditions:

  • "Start the task only if the computer is on AC power" and "Stop if the computer switched to battery power".

Settings:

  • "Allow task to be run on demand" and "if the running task does not end when requested, force it to stop".

Solution

  • @Dr Mouse, try this. I was able to run your code (while logged off). FYI, I am running Anaconda on Windows as well.

    Once in the Edit Action window (in your actions, that you showed above, click on the edit button), in the program/script, be sure that it is pointing to the actual instance of your Python (Anaconda).

    Put double quotes around the Program/Script

    "D:\Anaconda3\python.exe"
    

    Do not copy and paste, type it in.

    In the add arguments optional:

    -u "D:\projects\foo\autopull.py"
    

    In Start in (optional) add the folder location to the script without quotes:

    D:\projects\foo
    

    If the above doesn't work, for troubleshooting purposes:

    • Does it work when you click on run (which runs it immediately) in task scheduler (naturally while logged in)?
    • In task scheduler, did you look through the history of the task to see where the scheduler is failing?

    EDIT: Thinking more about it, I remember there was some issue with running python directly using the "Run whether user is logged on or not".

    If the above does not work, try this:

    create batch file (.bat) and add this line in it:

    D:\Anaconda3\python.exe D:\projects\foo\autopull.py %*

    point the task scheduler to this batch file (instead of pointing it to the python script directly).