Task Scheduler on Windows Server: Running Multiple Instances of a Python Script
Task Scheduler is a powerful tool in Windows Server that allows administrators to automate various tasks, including running scripts and programs at specific times or on system startup. In this article, we will focus on a specific use case where a startup task runs a Python script every minute using Git Bash.
Prerequisites
Before we dive into the details of running multiple instances of a Python script using Task Scheduler, it is essential to ensure that you have the necessary tools and software installed on your Windows Server.
- Python: You can download and install the latest version of Python from the official website: https://www.python.org/downloads/
- Git Bash: You can download and install Git Bash from the official website: https://git-scm.com/downloads
- Task Scheduler: Task Scheduler is a built-in tool in Windows Server. You can access it by searching for "Task Scheduler" in the Start menu.
Creating a Python Script
Before we can create a Task Scheduler task, we need to create a Python script that we want to run. For this example, we will create a simple script that prints the current date and time.
import datetime
print(datetime.datetime.now())
Creating a Task in Task Scheduler
Now that we have our Python script, we can create a Task Scheduler task to run it every minute.
- Open Task Scheduler and click on "Create Basic Task" in the right-hand Actions panel.
- Enter a name and description for your task and click "Next".
- Select "When the computer starts" as the trigger and click "Next".
- Select "Start a program" as the action and click "Next".
- In the "Program/script" field, enter the path to your Git Bash executable. In our example, it is "C:\Program Files\Git\bin\bash.exe".
- In the "Add arguments" field, enter the path to your Python script, followed by the word "&" and the word "sleep" and the number of seconds between each run of the script. In our example, it is "
C:\path\to\script.py & sleep 60". - Click "Next" and "Finish" to create the task.
Running Multiple Instances of a Python Script
Now that we have created our Task Scheduler task, it will run our Python script every minute. However, if we want to run multiple instances of the script simultaneously, we need to modify the task slightly.
- Open Task Scheduler and find your task in the Task Scheduler Library.
- Right-click on your task and select "Properties".
- Go to the "Actions" tab and click "New".
- In the "Program/script" field, enter the path to your Git Bash executable. In our example, it is "C:\Program Files\Git\bin\bash.exe".
- In the "Add arguments" field, enter the path to your Python script, followed by the word "&". In our example, it is "
C:\path\to\script.py &". - Click "OK" to create the new action.
- Click "OK" again to close the task properties window.
Now, when the task runs, it will start a new instance of Git Bash for each action, allowing multiple instances of the Python script to run simultaneously.
- Task Scheduler is a powerful tool in Windows Server that allows administrators to automate various tasks.
- You can use Task Scheduler to run a Python script every minute using Git Bash.
- To run multiple instances of a Python script simultaneously, modify the Task Scheduler task to start a new instance of Git Bash for each action.