0

I have set up my windows task scheduler to create a task to run a python script that will send out an email to a few people (including myself).

I added the location the location of my Python in the actions tab in Program/script

C:\Users\User\AppData\Local\Programs\Python\Python37\python.exe

I added my python file as an argument and the path to the file as Start in:

C:\Users\User\Documents\GitHub\automation

However when I run the script (I am testing it by running it every 5 minutes for 15 minutes). Python.exe opens up briefly but the emails in my python script are not sent, which means the script isn't running. I have tested my code several times and know that it works.

martineau
  • 119,623
  • 25
  • 170
  • 301
Emm
  • 2,367
  • 3
  • 24
  • 50
  • That (most likely) means that there is an _error in your code_ that prevents `Python` from sending the email. Try executing your program in the command prompt. (`win + r`, than type `cmd.exe` and enter) – Unsigned_Arduino Jun 12 '20 at 21:06
  • @Unsigned_Arduino the code runs in my IDE terminal but I get the error 'No module named 'gspread' when I try run it in cmd.exe. Guessing I have to add all the libraries I imported to a requirements.txt and import that in my code? – Emm Jun 12 '20 at 21:15
  • 1
    You will have to install the library globally, like do a `pip install (your module name)` in the command prompt – Unsigned_Arduino Jun 12 '20 at 21:17
  • 1
    Could be a credentials issue. See [my answer](https://stackoverflow.com/a/4209102/355230) to slightly different Windows scheduling question. – martineau Jun 12 '20 at 21:39
  • 1
    The first step in diagnosing this problem is to have the task scheduler run a script that creates an output file at a fully-qualified location. Write some basic information in the file such as `os.getcwd()`, `sys.executable`, and `sys.path`. Try to import packages that you expect to be available, and handle `ImportError` to note any missing packages. – Eryk Sun Jun 13 '20 at 00:50

2 Answers2

2

Most likely the task scheduler runs under a service account in Windows. This means it won’t have access to all the packages you installed locally. To remedy this, try

pip freeze —user > requirements.txt

Then in an admin command window:

pip install requirements.txt

This will reinstall packages so that they are available for all users

Krumelur
  • 31,081
  • 7
  • 77
  • 119
  • The Task Scheduler is a service that's hosted in an svchost.exe process. This process runs as SYSTEM in session 0 and hosts many similar services such as the Secondary Logon service. Task Scheduler can run a task as any user, in either an interactive session that belongs to the user, or in session 0 if the task is configured to allow it and the user has the batch logon right. If "python.exe opens up briefly" means that Emm sees a console window flash briefly, then the task is configured to run using Emm's account when Emm is logged on in an interactive session. – Eryk Sun Jun 13 '20 at 01:02
  • Cool, but a session ID is not the same as a user ID. I believe you can run processes on a different UID in the same session, but I am not sure. There is the “runas” command for example. – Krumelur Jun 13 '20 at 05:32
  • An interactive session has a primary access token and logon, which can be queried via `WTSQueryUserToken`. Task Scheduler uses this logon when running interactively. It does not support a secondary logon in the session of another user, unlike runas.exe. Which session would it use -- an arbitrary session? No way. If the user has the batch logon right, Task Scheduler can run a task in session 0 with the user's stored password used for the batch logon. It can also implement the latter without a stored password by using an S4U logon, but the task won't have access to remote resources. – Eryk Sun Jun 13 '20 at 06:07
  • Note that the key here is to run the `pip install` command(s) as an ADMINISTRATOR. My scheduled tasks run as a different user to my usual login account, and the Python scripts were succeeding for me, but failing in the scheduled task. I uninstalled the Python modules (which were installed locally) and the re-installed them as ADMINISTRATOR and this resolved the problem for me (I was not aware of the `pip freeze -user ...` technique in this answer, which looks like it does something similar). – Son of a Beach Jan 31 '23 at 21:40
1

If you check the history for that task in your task scheduler, you'll see the exit code returned by the python interpreter. Most likely, this is either a permission issue (the user account you've configured the interpreter to run as does not have sufficient privileges to execute all the statements contained in the script) or a missing requirement. You mentioned in the comments that you were using an IDE; if that's the case, then you're likely using a virtual environment in your IDE and therefore the system installation of python is missing the package that you were using to run the code, leading to the emails not being sent out.

You can resolve any dependency errors by making sure to run pip install -r <requirements.txt file location> if you have a requirements.txt file specified in your IDE project, or you can simply manually install the required packages with similar pip commands. If the issue is Windows permissions, you can create a new Windows user with higher permissions (given that you have admin access on your own system) and have the task run as that user, or you can enable the Run with highest privileges check-button property in your task.

Stephen
  • 1,498
  • 17
  • 26