2

I have a file, script.py, I would like to schedule a Task Scheduler on my Windows 10 machine to run the script.py every 5 minutes. However, I have some parameters that I want to pass into the script.py file to run it:

python script.py --parameter --parameter < database.txt

I'm not sure how to implement these parameters to the Windows Task Scheduler.

1
  • I think the easiest is if You were to use a file where You store those parameters and add code to script.py that will read that file and add those parameters that way, it will also allow to edit them on the run Commented Jun 16, 2021 at 22:27

3 Answers 3

2

Go to properties of the task.

Under the action tab, add/edit an action.

And you will see options to include arguments/parameters.

enter image description here

That being said, windows task scheduler has always been a hit-or-miss for me. I personally find that using custom schedulers by code is more reliable.

Here is my suggestion:

Step 1) Make a bat file with the following contents in your code's working directory:

"C:\Users\user2\AppData\Local\Programs\Python\Python39\python.exe" "C:\Users\user1\Desktop\working-directory\runner.py"

and let's name it something like "start-runner.bat". Also, note that the "runner.py" here is your custom scheduler code and not your actual code.

In the "runner.py", you can use the subprocess module combined with the threading module to asynchronously call your main file "python script.py --parameter --parameter".

And instead of using a simple pipe to redirect output, you can use subprocess module features and redirect the stdout to a file.

For scheduling, import time and use "time.sleep(300)"

Step 2) Make the bat file read-only

Step 3) Copy the bat file object to your windows clipboard

Step 4) Type "shell:startup" in your run command

Step 5) Right-click and "paste shortcut" into the startup folder

Step 6) Make the shortcut read-only

Sign up to request clarification or add additional context in comments.

Comments

0

I think you open the Edit Action window for your action and under setting, there is a add arguments(optional) just fill in your paremeters.

Comments

0

Simply create one ".bat" file using python and write code to trigger that script.

suppose demo.bat triggers helo.py with arguments like arg1, arg2 and arg3

content of demo.bat should be

python /path/to/helo.py %1 %2 %3

and in your main python file, schedule task using python like

import os
os.system("schtasks /create /sc minute /mo 5 /tn task_name /tr 'path_to_demo.bat arg1 arg2 arg3' /st 12:00")

This will trigger demo.bat after every 5 minutes and demo.bat will trigger helo.py with arguments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.