Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to add a task to the windows task scheduler via python 3?

I have a main python script that generates a GUI, and through that GUI I want the user to be able to create, amend, and delete schedules managed by the windows task scheduler.

like image 534
I_do_python Avatar asked Oct 02 '14 12:10

I_do_python


People also ask

How do I Create a task in Task Scheduler in Python?

Double-click on the Task Scheduler, and then choose the option to 'Create Basic Task…' Type a name for your task (you can also type a description if needed), and then press Next. For instance, let's name the task as: Run Hello World. Choose to start the task 'Daily' since we wish to run the Python script daily at 6am.

Can Task Scheduler run a Python script?

Yes, you can execute a Python script with Windows Task Scheduler. If your script works using the command prompt, you can schedule your script to run at a specific time and date.


3 Answers

Just to round out the option list here... How about just calling the windows command line?

import os

os.system(r'SchTasks /Create /SC DAILY /TN "My Task" /TR "C:mytask.bat" /ST 09:00')

You can launch any executable, batch file, or even another python script - assuming the system is set to execute python...

schtasks has a rich list of options and capabilities...https://docs.microsoft.com/en-us/windows/win32/taskschd/schtasks

like image 85
jaytate Avatar answered Sep 28 '22 00:09

jaytate


This code creates a task which will run in 5 minutes (uses pywin32):

import datetime
import win32com.client

scheduler = win32com.client.Dispatch('Schedule.Service')
scheduler.Connect()
root_folder = scheduler.GetFolder('\\')
task_def = scheduler.NewTask(0)

# Create trigger
start_time = datetime.datetime.now() + datetime.timedelta(minutes=5)
TASK_TRIGGER_TIME = 1
trigger = task_def.Triggers.Create(TASK_TRIGGER_TIME)
trigger.StartBoundary = start_time.isoformat()

# Create action
TASK_ACTION_EXEC = 0
action = task_def.Actions.Create(TASK_ACTION_EXEC)
action.ID = 'DO NOTHING'
action.Path = 'cmd.exe'
action.Arguments = '/c "exit"'

# Set parameters
task_def.RegistrationInfo.Description = 'Test Task'
task_def.Settings.Enabled = True
task_def.Settings.StopIfGoingOnBatteries = False

# Register task
# If task already exists, it will be updated
TASK_CREATE_OR_UPDATE = 6
TASK_LOGON_NONE = 0
root_folder.RegisterTaskDefinition(
    'Test Task',  # Task name
    task_def,
    TASK_CREATE_OR_UPDATE,
    '',  # No user
    '',  # No password
    TASK_LOGON_NONE)

More info on tasks and their properties here: https://learn.microsoft.com/en-us/windows/desktop/taskschd/task-scheduler-objects

like image 13
xuhcc Avatar answered Oct 17 '22 12:10

xuhcc


PyWin32 provides an interface to the Task Scheduler in win32com.taskscheduler. You can see an example of it's use here:

  • https://github.com/SublimeText/Pywin32/blob/master/lib/x32/win32comext/taskscheduler/test/test_addtask_1.py

Also @FredP linked to a good example that's much simpler:

  • http://blog.ziade.org/2007/11/01/scheduling-tasks-in-windows-with-pywin32/

There is also an interesting tidbit in the wmi module's cookbook about scheduling a job, although it doesn't appear to use the Task Scheduler:

  • http://timgolden.me.uk/python/wmi/cookbook.html#schedule-a-job
like image 7
Mike Driscoll Avatar answered Oct 17 '22 13:10

Mike Driscoll