- 1 year ago
Python task scheduler via win32com on windows startup
To create a Python task scheduler using win32com
on Windows startup, you can use the Windows Task Scheduler API provided by win32com.client
to create a scheduled task that runs your Python script at startup. Here's a step-by-step guide:
- Install Required Libraries: Ensure you have the
pywin32
library installed. If you haven't installed it yet, you can do so usingpip
:
bashpip install pywin32
Create Python Script: Create a Python script that you want to run at startup. For example, let's assume your Python script is named
my_startup_script.py
, and it is located in the directoryC:\path\to\my\script
.Create a Batch File: Since we want to run the Python script at startup, we need to create a batch file that will call the Python interpreter and run the script. Create a new text file named
startup.bat
with the following content:
batch@echo off
pythonw.exe C:\path\to\my\script\my_startup_script.py
The pythonw.exe
command runs Python in a windowless mode, which means no console window will appear when the script is executed.
- Create Scheduled Task: Now, we will use
win32com
to create a scheduled task that runs thestartup.bat
batch file at startup. Here's the Python script to create the task:
pythonimport os
import win32com.client as win32
def create_startup_task():
task_xml = """
<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<Triggers>
<BootTrigger>
<Enabled>true</Enabled>
</BootTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>NT AUTHORITY\SYSTEM</UserId>
<LogonType>ServiceAccount</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>Parallel</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>true</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\path\to\my\script\startup.bat</Command>
</Exec>
</Actions>
</Task>
"""
# Save the task XML to a temporary file
temp_file_path = os.path.join(os.environ['TEMP'], 'startup_task.xml')
with open(temp_file_path, 'w') as f:
f.write(task_xml)
# Use Task Scheduler API to register the task
scheduler = win32.Dispatch('Schedule.Service')
scheduler.Connect()
root_folder = scheduler.GetFolder('\\')
task_definition = scheduler.NewTask(0)
task_definition.XmlText = task_xml
root_folder.RegisterTaskDefinition('MyStartupTask', task_definition, 6, '', '', 3)
# Delete the temporary XML file
os.remove(temp_file_path)
if __name__ == '__main__':
create_startup_task()
Replace C:\path\to\my\script
with the actual path to the directory containing your Python script and the startup.bat
file.
- Run the Python Script: Save the Python script with the name
create_startup_task.py
, and then run it using the Python interpreter. This will create the scheduled task that will run your Python script at startup.
After completing these steps, the scheduled task will be registered on your system, and the Python script will run automatically at every startup. If you want to remove the task, you can do so manually using the Task Scheduler or modify the Python script to include a method for removing the task.