Run a python script scheduled on a Windows server

In a lot of cases we want to run a script scheduled to do a certain task every “x” minutes or perhaps a heavy hitter export/import job that should run at night when the load on our systems is low.

My example script is just displaying a messagebox via tkinter, so we can easily see that the script actually ran after we kicked off the scheduled task:

from tkinter import messagebox
messagebox.showinfo(“Title”, “Hello World”)

On a Windows server I found to viable options. The first one has the advantage that the user creating the scheduled task does not need any understanding of Python whatsoever, since we are just creating a batch file. In the .bat file, we are pointing to the location where Python is installed and to the location where the script is stored.

 

Option #1 – A batch file to run our python script

 

  • Open a text editor of your choice
  • Add “<yourpython.exe location>” “<yourscript location>”

“C:\Program Files (x86)\Python37-32\python.exe” “C:\Temp\My Scriptlocation\Scheduled_msg_box.py”

  • Save the file as a .bat file
  • Hit your Win+R keys and write “taskschd.msc”
  • Right click and chose “Create Basic Task”

create basic task

  • Add a name and description to the scheduled task(Remember a good naming convention and documentation will save you and everyone else a lot of time in the future)
  • Chose when the task is supposed to run
  • Chose “Start a program” under the Action tab
  • Now click browse and select your batch file

Basic task

  • Confirm with Next and finish the configuration
  • Now its time to test our scheduled task, select the task we just created and click “run”

run task

 

Option #2 – Using the python.exe and point to the script

Here we don’t use a batch file to run python, instead we use the python.exe directly to interpret our script from the Task Scheduler.

  • Create a basic task and chose again “Start a program”
  • Now we need to define the location where our python.exe is located or if we have added python to our OS Path variable, we can just use python(like in my example below)
  • To define the script python is supposed to run, we use the “Add arguments” field. Windows expects the whole path with quotation marks:

pythonargument

Testdrive again:

runpythonok

 

If your task is not running or/and you are getting error messages, here are some possible causes that you should check out:

  • Permissions to run the script.
    • Check out the permissions of the user who created the task and under which user the task is running
    • A user needs the Log on as Batch Job Rights to run a scheduled task
  • Path of the python.exe and the script, dont forget the quotation marks
    • If you want to run your script in a specific location(for example if a path to a logfile directory is not defined within your script), then this path works only without quotation marks:

no Quotation Marks

 

Cheers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s