Search code examples
androidpythonpython-3.xautomation

run python script in windows scheduled task


I use this repo to backup my postgers db.

I run it manually by this command :

python postgres_manager.py  --configfile psql.config --action backup --verbose VERBOSE

how can i run this script by windows scheduled task


Solution

  • As mentioned by tomdevin, using Windows task scheduler works nicely. However, I generally use batch files (.bat) to schedule. In the batch file I write the call to python.

    For example, here I activate the python virtual environment, move to the project folder and then call the python script main.py with 2 command line options. After running the script it calls an 'EXIT' statement to exit the terminal again.

    call C:\venv\Scripts\activate.bat && cd C:\GIT\ProjectFolder && C:\venv\Scripts\python.exe C:\ProjectFolder\main.py --dataloc "C:/Data/" --saveloc "C:/Output/"
    EXIT
    

    You can simply copy paste this in a Notepad and save it as 'run.bat' or whatever you like. Then, from task scheduler under actions, you call 'start a program' and point to your .bat file.

    This method provides some more flexibility and allows easy use of virtual environments.