Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Windows Task Scheduler through Python

I am new to SO. I am given the task of creating a windows task schedule to run a .BAT file using our Python API to push it to our fleet of remote PCs.

I am having issues with passing the arguments over to the schtask.exe.

Here is the code:

import subprocess
path = "c:\windows\System32\schtasks.exe"
subprocess.Popen([path, "schtasks /create /SC ONLOGON /TN 'Update_Automation_Beta' /TR 'C:\test\run_admin.bat'"], shell = True)

Note: The task is just a test task right now, while I try to figure it out. Also if typed directly into command prompt window, it will work, removing the quotes etc.

like image 845
KJ Jr Avatar asked May 26 '15 14:05

KJ Jr


People also ask

Can you run a Python script in task scheduler?

In order to schedule the Python script using the Windows Scheduler: Open the Windows Control Panel and then click on the Administrative Tools. Double-click on the Task Scheduler, and then choose the option to 'Create Basic Task…'

How do you make Python code run automatically?

Step 1: Open Task Scheduler Application on your Windows Machine. Step 2: Click on 'Create Basic Task…. ' in the Actions Tab. And give a suitable Name and Description of your task that you want to Automate and click on Next.


1 Answers

This worked for me:

import subprocess
subprocess.call('schtasks /create /SC ONLOGON /TN "Update_Automation_Beta" /TR "C:\test\run_admin.bat"')

Use single quotes outside and double qoutes inside. Also you can put in the full path to schtasks, if you need.

like image 133
abuzze Avatar answered Oct 03 '22 23:10

abuzze