Python Forum
Running Python script through Task Scheduler? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Running Python script through Task Scheduler? (/thread-41737.html)



Running Python script through Task Scheduler? - Winfried - Mar-10-2024

Hello,

This is my first try at running a Python (3.12.0) script through Windows 10's Task Scheduler… and it didn't work (as pretty much expected).

It's a simple script that 1) fetches a web page from the Net, 2) parses it to find something, and 3) sends an email through my ISP's SMTP server. The script works when launched manually.

First, I simply fed it the full path to the script: No email.

Next, I right-clicked on it, told TS to use the following app to run the script… still no email.
Quote:C:\Users\joe\AppData\Local\Programs\Python\Python312\python.exe

Does someone know/have a guess at what's wrong?

Thank you.

[Image: Task-Scheduler-Python.png]


RE: Running Python script through Task Scheduler? - deanhystad - Mar-10-2024

Does your script work when you run it from a terminal, line from CMD or powershell?


RE: Running Python script through Task Scheduler? - snippsat - Mar-10-2024

This is just your path to Python,that shall in Program/script
Quote:C:\Users\joe\AppData\Local\Programs\Python\Python312\python.exe
Then in argument Add argument(absolute path to your Python code)

When test just use simple code that do something visible.
# open_notpad.py
import subprocess

subprocess.Popen(['notepad.exe'])
Create a new task eg name it Open Notepad next>next and input only the two Paths(Python and open_notpad.py) .
Then can go in and edit the created task.
Under settings choose Daily and set the start date and time to some short time in future.
Click on Repeat task every and change manually to 1 minutes from the dropdown.
Then change the duration to Indefinitely.
Now if work every minute notepad start.
Then just delete this task,and do process for the other task.


RE: Running Python script through Task Scheduler? - Winfried - Mar-10-2024

Thanks.

Yes, like I said, "The script works when launched manually."

After I followed the instructions above, I can see the screen flicker every minute (looks like a CMD window opens and closes real fast) but I don't see Notepad.

If there's an easier way to run a Python script on Windows once every day, I'm not wed with MS' Task Scheduler.

[Image: image.png]


RE: Running Python script through Task Scheduler? - Pedroski55 - Mar-10-2024

Quote:The script works when launched manually.

Recently, can't say when exactly, not long ago, there was a post about something working from the command line, but not in Idle.

deanhystad said, the problem was probably Idle, and the OP tried some other IDE and it worked! (I tried it in Idle and it worked for me.)

Maybe your Task Scheduler, whatever that is, is the source of the problem?


RE: Running Python script through Task Scheduler? - deanhystad - Mar-10-2024

Try running the script in cmd or powerscript when you are in a different folder. Try running as a different user.


RE: Running Python script through Task Scheduler? - Winfried - Mar-10-2024

What a pain…

What about using PyInstaller to compile the whole thing into a single binary?


RE: Running Python script through Task Scheduler? - snippsat - Mar-10-2024

(Mar-10-2024, 06:03 PM)Winfried Wrote: After I followed the instructions above, I can see the screen flicker every minute (looks like a CMD window opens and closes real fast) but I don't see Notepad.
Then it probably working.
Change to this and test again,now it will force cmd to stay open.
import subprocess

subprocess.Popen(['C:/Windows/System32/notepad.exe'])
input('Press enter to exit')
Quote:If there's an easier way to run a Python script on Windows once every day, I'm not wed with MS' Task Scheduler.
Python job scheduling for humans
# pip install schedule
import schedule
import time

def job():
    print("I'm working...")

schedule.every(.1).minutes.do(job) # Woking test 10 sec
# schedule.every().day.at("10:30").do(job)
while True:
    schedule.run_pending()
    time.sleep(1)



RE: Running Python script through Task Scheduler? - Winfried - Mar-10-2024

I'll try that instead.

Thank you!