Running a python script through Windows Scheduler not working
Question:
I am trying to automate a python script through the Windows Task Scheduler but its not working. At the end of my python script. two CSV files should be created but they arent.
I tried the following:
1. Copied the address of my python.exe to Program/Script.
C:Program FilesPython35python.exe
- In the Add arguments, i put the name of my file
Historical Aggregation.py
- In the Start in (optional), i put the path of my python script
C:UsersmynamePycharmProjectsProject1
Am I missing something
Answers:
To simplify, we can create a really short .bat
file, that will only receive the necessary command to run your python script.
To do so, try this:
Create a executePy.bat
file in the same folder than your Python file (C:UsersmynamePycharmProjectsProject1), with content:
@echo off
"C:Program FilesPython35python.exe" "Historical Aggregation.py"
Then, on your task scheduler, simply schedule a test with Program/Script:
"C:UsersmynamePycharmProjectsProject1executePy.bat"
Leave Add Arguments
and Start In
in blank. Now, your task should be ready to run.
Another approach would be to set fields as:
- Program/Script – your python path (with quotation marks):
“C:Program FilesPython35python.exe”
- Add arguments – full file name of the script, including it’s path (with quotation marks):
“C:UsersmynamePycharmProjectsProject1Historical Aggregation.py”
I had a very similar issue, and solved it in a different way. Here my step by step guide:
-
Transform the python script to an .exe, using in the DOS cmd prompt the command:
pyinstaller -- onefile [name of the file.py]
-
Place the CSV file that you want to update in the same folder as the .exe file created
-
Create a basic task on Windows Scheduler, with the following properties:
-
General – select
- Run whether user is logged on or not
- add the PC password. For my PC, use the user name DESKTOP-M40FS79dario and the PC password
- Run with highest privileges
-
Triggers – select
- Daily
- Repeat task every 30 minutes
- Stop task if it runs longer than 15 minutes
-
Actions
- Under Program/ Script insert the path to your .exe file, for instance, C:Pythondatadisttest.exe
- Under Start in (optional) insert the path to the directory where the CSV and .exe files are, for instance C:Pythondatadist
-
Conditions – select
- Start the task only if the computer is on AC power, and make sure you have connected the power
- Wake the computer to run this task
-
Settings – leave the default options
-
Save the task by inserting the password
-
Leave the task status on “Ready”
Good luck!
I have a problem with the task exiting.. It runs, but then it says "task is currently running". It’s a 1 second python script… should run and end.. I’m using this for code:
C:temppythonfile.bat
and it’s content is:
@echo off
C:/Users/benjune/AppData/Local/Programs/Python/Python310-32/python.exe C:temppythonbenfile.py
It’s scheduled to run every 5 minutes, and should finish really quickly. Should I use an exit or something after that?
I am trying to automate a python script through the Windows Task Scheduler but its not working. At the end of my python script. two CSV files should be created but they arent.
I tried the following:
1. Copied the address of my python.exe to Program/Script.
C:Program FilesPython35python.exe
- In the Add arguments, i put the name of my file
Historical Aggregation.py
- In the Start in (optional), i put the path of my python script
C:UsersmynamePycharmProjectsProject1
Am I missing something
To simplify, we can create a really short .bat
file, that will only receive the necessary command to run your python script.
To do so, try this:
Create a executePy.bat
file in the same folder than your Python file (C:UsersmynamePycharmProjectsProject1), with content:
@echo off
"C:Program FilesPython35python.exe" "Historical Aggregation.py"
Then, on your task scheduler, simply schedule a test with Program/Script:
"C:UsersmynamePycharmProjectsProject1executePy.bat"
Leave Add Arguments
and Start In
in blank. Now, your task should be ready to run.
Another approach would be to set fields as:
- Program/Script – your python path (with quotation marks):
“C:Program FilesPython35python.exe”
- Add arguments – full file name of the script, including it’s path (with quotation marks):
“C:UsersmynamePycharmProjectsProject1Historical Aggregation.py”
I had a very similar issue, and solved it in a different way. Here my step by step guide:
-
Transform the python script to an .exe, using in the DOS cmd prompt the command:
pyinstaller -- onefile [name of the file.py]
-
Place the CSV file that you want to update in the same folder as the .exe file created
-
Create a basic task on Windows Scheduler, with the following properties:
-
General – select
- Run whether user is logged on or not
- add the PC password. For my PC, use the user name DESKTOP-M40FS79dario and the PC password
- Run with highest privileges
-
Triggers – select
- Daily
- Repeat task every 30 minutes
- Stop task if it runs longer than 15 minutes
-
Actions
- Under Program/ Script insert the path to your .exe file, for instance, C:Pythondatadisttest.exe
- Under Start in (optional) insert the path to the directory where the CSV and .exe files are, for instance C:Pythondatadist
-
Conditions – select
- Start the task only if the computer is on AC power, and make sure you have connected the power
- Wake the computer to run this task
-
Settings – leave the default options
-
-
Save the task by inserting the password
-
Leave the task status on “Ready”
Good luck!
I have a problem with the task exiting.. It runs, but then it says "task is currently running". It’s a 1 second python script… should run and end.. I’m using this for code:
C:temppythonfile.bat
and it’s content is:
@echo off
C:/Users/benjune/AppData/Local/Programs/Python/Python310-32/python.exe C:temppythonbenfile.py
It’s scheduled to run every 5 minutes, and should finish really quickly. Should I use an exit or something after that?