Unable to Correctly Run Python Scripts Through Scheduler

1

So I've researched and tested multiple methods to get this to work, but both our IT guy and myself are unsure how to proceed. Ultimately I need to run three python scripts on a server which look at a set of Excel files and scans them for errors. If there are errors, it will email the appropriate people to fix them. The scripts themselves work fine when run through the CMD prompt, either individually or in a batch file. These scripts work perfectly when run manually from the command prompt.

However, when I try to schedule one of the scripts or the batch file, they do not run in their entirety. I have a print statement early on in the scripts and that runs fine in the Python.exe, but then the script abruptly quits. This makes me think that Python runs fine, but the contents of the script aren't running correctly through the scheduler.

To simplify troubleshooting I created two test scripts, test.py and test2.py, and placed them in a simple directory of the server, C:\Store. All this first script does is print something to the console, and write something to a file and save it to the root directory, after which it will wait for user input. The second script is simpler and just prints the architecture of Python and it's build path. Both scripts work fine when run from the command prompt manually.

**test.py** Does NOT Work as scheduled task
import io

print('JUST WORRRRRKKK!')

with open("//kaicmapp/Store/foobar.txt", "w") as file:
    file.write("Hello!")

    file.close()

raw_input("press any button")



**test2.py**  Works as scheduled task
import sys  
import platform  

print("Python EXE     : " + sys.executable)  
print("Architecture   : " + platform.architecture()[0])   

raw_input("\n\nPress ENTER to quit")  

What I am using for these test scripts:

  • Python 2.7 32 bit
  • Windows Server 2012
  • Windows User Profile with Admin Rights
  • Python 2.7 exe added as environment variable. Renamed to python2.exe in installation directory, as I also have Python 3 installed on the serverand want to be able to run them exclusively

Things I've tried:

  • Running Task Scheduler as Admin
  • Run with/without highest privileges
  • Run only when current user is logged in
  • Run regardless of current user logged in, password entered correctly
  • Run the scripts in a batch file
  • The solution listed here, results in same outcome in both scripts

Things I've noticed:

  • Using python2 "C:\Store\test2.py" in the scheduler will run the script successfully, with the prompt waiting for user input. Run result from the scheduler says The operation completed successfully. (0x0)
  • Using the same task as above, but running test.py instead, will not result any output from the Python shell, it simply pops up and disappears. Run result from scheduler says Incorrect function. (0x80070001)

Any ideas on how to proceed? I am not very familiar with server administrative tasks, so I feel pretty lost here.

python
python-2.7
scheduled-tasks
windows-server-2012
asked on Stack Overflow Jul 27, 2017 by Gthoma2 • edited Jul 28, 2017 by Ivan Kolesnikov

1 Answer

1

User contributions licensed under CC BY-SA 3.0