Get More Done, Optimize Your Time and Automate Your Tasks with Windows Task Scheduler
Sahil Kavitake
Data Scientist | ML-DL | GEN-AI | LLM | Python | Full Stack | Enhancing Decision Making through Data Analysis
Windows Task Scheduler is a powerful tool that allows you to schedule and automate various tasks on your computer. Whether you need to run a script at a specific time or on a recurring schedule, Windows Task Scheduler makes it easy to set up and manage your tasks. This is the best way to schedule a function to run at a specific time of the day without using CRON job or cloud platform.
What is a script?
A script is a set of instructions or commands that are executed by a computer program or interpreter. In the context of programming, a script typically refers to a small program or program segment that is written in a scripting language.
Steps to Schedule Python Script using Windows Scheduler
Step-1: Prepare the Python Script
The below script simply imports the datetime module and defines a function "print_current_time" which uses the datetime.now() function to get the current time and print it. The last line allows the script to be run as a standalone program, printing the current time when the script is executed.
import datetime
def print_current_time():
? ? current_time = datetime.datetime.now()
? ? print("The current time is:", current_time)
if __name__ == "__main__":
? ? print_current_time()
Once you’re done writing the script, save it as a Python file (where the file extension is?.py):
For instance, let’s save the file as?showtime.py?under the any path:
C:\Users\Sahil\Desktop\showtime.py
Step-2: Create Batch File to Run the Python Script
A batch file, also known as a batch script or .bat file, is a plain text file that contains a series of commands or instructions to be executed in sequence by the command prompt (cmd.exe) in Windows. These commands can include built-in Windows commands, external programs, and custom scripts. Batch files are often used to automate repetitive or complex tasks, such as running programs, performing system administration tasks, or making changes to multiple files or directories.
We are going to make a batch file to run our python script.
"Path to Python exe\python.exe" "Path to Python script\script_name.py
pause"
In our case:
"C:\Users\Sahil\AppData\Local\Programs\Python\Python310\python.exe" "C:\Users\Sahil\Desktop\showtime.py" pause
Finally, save the file with the “bat” file extension: python_script.bat under the any path:
C:\Users\Sahil\Desktop\python_script.bat
Get Started Using Windows Task Scheduler
To run your?bat file you will need to create a task, create an action, add the path to your python script and add a trigger to schedule your script.
1.Create a new task
Open the task scheduler application on your computer by searching for Task Scheduler in the start menu.
After opening the application it should look something like this:
Click “Create Basic Task…” in the Actions tab on the top or on the right.
You should put your task name (e.g. Show Time) and description (e.g. Show time automatically every day at 6 pm) and click Next>
2.Choose your trigger time
We will pick daily.
When scheduling a task in Windows, you have the option to choose a time trigger, such as daily, weekly, or monthly. Choose the frequency with which you want to want your task to be executed. The frequency will depend on the specific requirements of your task and the data source you are working with. For instance, if your task is to scrape a financial website for stock balance sheets, you may choose to run the script every quarter to ensure that you are always working with the most recent data.
Pick the exact time for our previous selection
Choose the start date and time when you want the task to be triggered. Also, choose after how many days you want the task to repeat/recur and click next. I set it to run?from today?at 6 PM time and to?repeat the task?everyday.
Click Next.
?? Quick Tip: You may see different options based on?your previous selection.
Choose an Action
Here you will be able to start the Python Scripts, send an e-mail, and even display a message. Feel free to choose ones which you are most comfortable with. However, you should watch out as there are deprecated tasks which will be removed in the subsequent patches.
Choose “Start a program” and click Next.
Add your program script where you saved your bat file earlier.
Click Next and Lastly, check all the entered values and click Finish.
Now, at your chosen time, your script will execute.
?? Congratulations, you have set up your first automated scheduler in Windows. I advise you to try to run your script manually from the command line before creating a recurring task
Conclusion
That’s it, you now know how to run a Python script automatically using?Windows Task Scheduler.
These are just a few examples, and there are many other possibilities depending on the specific needs and preferences you can schedule. The key is to identify tasks that can be automated and scheduled to run at specific times or intervals, to save time and increase productivity.
I hope you have found this article informative and helpful in your quest to schedule Python scripts on Windows. Your feedback and suggestions are highly valued, so please feel free to leave a comment below with your thoughts and ideas. Your input will help to make this article even better.
Be creative ?! Think of all the tasks you do every day. I’m sure some of them can be automated.
Senior Account Manager @ StudyAbroad7 / Invictus Engineers|
1 年Automating repetitive tasks frees up valuable time for strategic initiatives, thanks for sharing @Sahil Kavitake
Business Analyst at Zomato
1 年A really good piece, and very well researched!
Data Analyst | Business Analyst | Machine Learning | Python | SQL | Power BI
1 年Can you help me what is that problem
Data Analyst | Business Analyst | Machine Learning | Python | SQL | Power BI
1 年My python code execute manually it run and create table in database but while task scheduler it shows task completed but I didn't get any table in database