Get More Done, Optimize Your Time and Automate Your Tasks with Windows Task Scheduler

Get More Done, Optimize Your Time and Automate Your Tasks with Windows Task Scheduler

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.

  1. First, open up any text editor.
  2. Next, enter the following lines in an empty file:

"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.

No alt text provided for this image

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:

No alt text provided for this image

Click “Create Basic Task…” in the Actions tab on the top or on the right.

No alt text provided for this image

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>


No alt text provided for this image

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

No alt text provided for this image

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

No alt text provided for this image

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.

No alt text provided for this image

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.

  1. Regular virus scans of the computer using antivirus software.
  2. System maintenance tasks such as disk cleanup, defragmentation, and error checking.
  3. Scheduled system updates to keep the operating system and software up to date.
  4. Automatic downloads of software updates or patches for applications.
  5. Running scripts or batch files to automate repetitive tasks.
  6. Automatic shutdown or restart of the computer at a specific time.
  7. Scheduling the running of certain programs or applications at specific times or intervals.
  8. Launching certain websites or web applications at specific times or intervals.
  9. Scheduling regular reminders for upcoming events or deadlines.

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.

Irene Stella Pereira

Senior Account Manager @ StudyAbroad7 / Invictus Engineers|

1 年

Automating repetitive tasks frees up valuable time for strategic initiatives, thanks for sharing @Sahil Kavitake

Harsh Mishra

Business Analyst at Zomato

1 年

A really good piece, and very well researched!

Sambasivareddy S

Data Analyst | Business Analyst | Machine Learning | Python | SQL | Power BI

1 年

Can you help me what is that problem

回复
Sambasivareddy S

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

要查看或添加评论,请登录

Sahil Kavitake的更多文章

社区洞察