Home:ALL Converter>Windows task scheduler not running python script/batch file

Windows task scheduler not running python script/batch file

Ask Time:2016-02-18T19:36:00         Author:ratherstrange

Json Formatter

I have a very simple python script (to send an email) which works perfectly when I run it in the cmd window, or in python, or if I directly start a a .bat file pointing to it.

However, when I try to get the task scheduler to run it, nothing happens. The task scheduler says that it runs and completes successfully, the log file is blank, but no email is sent.

I'm aware there are a lot of other questions relating to this problem, and I've read through them and tried the solutions, but nothing seems to work. I am new to python (and to scheduling tasks!), so I may be implementing the solutions incorrectly.

Here is what I've tried...

  • Creating a batch file with the script in it in various ways:

    python C:\Users\me\Documents\etc\script.py >C:\Users\me\Documents\etc\log.txt
    
    python "C:\Users\me\Documents\etc\script.py >C:\Users\me\Documents\etc\log.txt"
    
    C:\Users\me\AppData\Local\Programs\Python\Python35-32\python.exe C:\Users\me\Documents\etc\script.py
    
    "C:\Users\me\AppData\Local\Programs\Python\Python35-32\python.exe" "C:\Users\me\Documents\etc\script.py"
    
    C:\Users\me\Documents\etc\script.py
    

All of these work fine when double clicking on the file - but none run in task manager. (Although they say they have completed successfully). In task manager I just put the link to the .bat file in the "Program/script" box.

  • Doing the above but with the full path to the cmd.exe in the program/script, and the .bat file as an argument. I've also tried putting the location of the bat file in the "Start in (optional):"

  • Trying to run the .py file directly via the task scheduler, putting it in the "Program/script:" box

  • Trying to run the .py file directly via the task scheduler, putting the full path to the python.exe (see above) in the "Program/script", and the sript.py in the "Add arguments". I've also tried this with path to the location of the script in "Start in".

  • Trying to run the .py file via the cmd - so putting the full path to the cmd.exe in "Program/script" and the script.py file (full path) in the "Arguments".

Some of the ones where I try to run the script.py directly just say "running" in the Task Scheduler forever, but I didn't note down which these were.

I'm running Windows 7 (64bit), and have got Python 3.5.1 (32bit). I've got local admin rights.

Other scheduled tasks I have created (not involving a python script) work fine, but this has stumped me. Please help!

Author:ratherstrange,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35480359/windows-task-scheduler-not-running-python-script-batch-file
ratherstrange :

Just adding an answer in case this affects any other newbie :). I needed to check \"Run only when user is logged on\", and also uncheck \"Run with highest privileges\". \n\nI'm guess this is because as eryksun says Outlook has a GUI.",
2016-02-19T10:12:50
Anton :

Something else to try:\n\nMake sure in your batch file you're adding a command to change directories to where your executable lives.\n\n@echo off\necho.------------------------------------------------ \necho.Windows Task invoked on %date%, %time% (local time)\necho.------------------------------------------------ \n\nSET My_exe_dir=\"C:\\Program Files (x86)\\MyProgram\\FolderWhereExeLives\"\n\nSET Input_dir=\"C:\\Program Files (x86)\\MyInputFolder\"\n\ncd %My_exe_dir% <-- This was the key for me.\n\n%My_exe_dir%\\myprogram.exe %Input_dir%\\MyInputFile.xml -1\n",
2017-08-04T13:27:32
yy