Running Python as a Scheduled Task (Windows)
To run python in the background is easy, to do it in the background and as a scheduled task is harder.
If you want to just run a script in the background, you can use
pythonw "script.py"
or change your script’s extension to .pyw, and then call the script. Here’s how to schedule a task
- Create
run.bat
in your project directory with the contentpython "script.py
- Open up Task Scheduler (search from the Start menu).
- On the left sidebar, click “Task Scheduler Library.”
- Click “Create Basic Task…”
- Enter the task name, description, and click next.
- Select your trigger, I needed “When the computer starts.”
- Action is “Start a program.”
- Program/script will be the bat file you created in Step 1
- Make sure “Start in” is your project directory.
- Click next, and check “Open the Properties dialog…”
- Have “Run whether user is logged on or not” checked (if your trigger is running when the computer starts).
- Have “Hidden” checked.
- Change “Configure for:” to the correct option. Windows 10, in my case.
- Tinker with the other options if you need to.
- Click “OK.” You will need to provide your password.
- Test the task by clicking “Run” on the right panel.
If the task did not execute (or it did but not in the background) it may be that you didn’t follow the steps properly (e.g. I forgot to set the configure for). I hope you found this helpful. Leave a comment or email me if you have any issues.