0
votes

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!

3
You shouldn't need cmd.exe or a batch file to run a Python script in the task scheduler. Just run python.exe directly, which I know you've tried, so the issue is something to do with your script. First try a simple test script that writes to a file.Eryk Sun
Does your script have any GUI elements or execute (directly or indirectly via COM) any program that has a GUI (e.g. Outlook)? If so you need to ensure that it's run "only when user is logged on".Eryk Sun
My script does use outlook - I changed the settings to "only when user is logged on" but no joy. taskeng.exe opens, but nothing happens and the task scheduler says it's running.ratherstrange
I'll try a different script now and see if that works.ratherstrange
You can test by running python.exe without a script. If you select a user that's logged on interactively, when you manually run the task it should open in a console window in that user's session. The user must be logged on interactively to create any window. Non-interactive tasks run in the services session (0), which is isolated from interactive sessions (1+).Eryk Sun

3 Answers

2
votes

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".

I'm guess this is because as eryksun says Outlook has a GUI.

0
votes

Something else to try:

Make sure in your batch file you're adding a command to change directories to where your executable lives.

@echo off
echo.------------------------------------------------   
echo.Windows Task invoked on %date%, %time% (local time)
echo.------------------------------------------------   

SET My_exe_dir="C:\Program Files (x86)\MyProgram\FolderWhereExeLives"

SET Input_dir="C:\Program Files (x86)\MyInputFolder"

cd %My_exe_dir%  <-- This was the key for me.

%My_exe_dir%\myprogram.exe %Input_dir%\MyInputFile.xml -1
0
votes

For me, specifying the "start in" directory worked:

Define Start in directory in properties of scheduled task. Start Windows Task Scheduler. Navigate to the task and double click on it to open the Properties of the task. Select tab Action and click on button Edit. There is Start in (optional). Enter here the path of the executed batch file. Click two times on button OK to save this important modification in properties.