top of page

How to Schedule a Python Script to Run Automatically

Featured image by Pixlr AI Image Generator

This post will explain how to schedule a python script execution using Windows Task Scheduler or Cronjobs, allowing you to automate tasks using python on both Windows and Mac.


How to Schedule a Python Script Using Windows Task Scheduler

  1. Open the Windows Task Scheduler GUI

  2. Actions > Create Task

  3. In the General tab, give a name for your scheduled task. If you change the Security options from Run only when user is logged on to Run whether user is logged on or not, the script will also run when the computer is sleeping. If the computer is powered off, the script will not run and will not catch up on missed executions if it is later powered on.

  4. In the Actions tab, click New...

  • Action: Start a program

  • Program/script: the location of python executable on your computer, for ex. C:\Users\81701\AppData\Local\Microsoft\WindowsApps\python.exe

    • Get the location by typing where python in command prompt

  • Add arguments: the name of your python file, for ex. yourfile.py

  • Start in: the file path to your python file, for ex. C:\Users\81701\python\yourfile.py

Lastly, trigger your script execution by navigating to the Triggers tab and clicking New...

How to Schedule a Python Script Using Cronjobs on Mac Crontab

  1. Open a terminal window. 

  2. Type the following command to edit the crontab file: crontab -e. This will open the crontab file in the default text editor, usually vi or vim. 

  3. Press the i key to enter Insert mode. 

  4. If there are a bunch of ~ characters, just delete them. They represent empty lines or lines that contain no visible characters. 

  5. Type in the cron job, for example 0 10 MON /path/to/python /path/to/script.py. Some cloud storage services, like OneDrive, might have restrictions on executing files directly from their synced folders. In such cases, move the file to a local directory before scheduling it. To get the file path, right click on the python file in Finder. Press the Option key, then choose 'Copy [filename] as Pathname.' On Mac, the path to python interpreter is usually /usr/bin/python3 

  6. Press the Esc key to exit Insert mode. 

  7. Type :wq to save and exit 

  • : character puts vi in command mode 

  • w command is for writing (saving) the file 

  • q command is for quitting vi 

 

How to check that the cronjob was created successfully? 

To check the crontab entries that you have created, open Terminal again and use the crontab -l command. 

 

What if the computer is powered off at the scheduled time?  

If the computer is sleeping or powered off, the cron daemon will not be able to execute the job. Cron jobs will not catch up on missed executions if you later log in. If you need to run a task even when the computer is powered off, use a cloud-based data platform such as Databricks.  

 

What if the script encounters error?  

There will be a notification "You have mail" in terminal. 

  1. Command mail to see the details 

  2. Command t to read the entire message. 

  3. Command delete * to delete all terminal mails 

  4. Command q to quit mail 

 

How to delete an existing cronjob?  
  1. Open a terminal window. 

  2. Type the following command to edit the crontab file: crontab -e 

  3. Delete the line that contains the cronjob entry. In vi or vim, navigate to the line and press dd to delete it. 

  4. Type :wq to save and exit 


Other ways to schedule a python script to run automatically?

bottom of page