Chapter 8. Scheduling Scripts to Run Automatically

We’ve covered a lot of ground up to this point in the book. After reviewing Python basics, we processed text files, CSV files, Excel files, and data in databases, and applied our new knowledge to three common business analysis applications. In these examples, we’ve run the scripts manually on the command line. For example:

python my_python_script.py input_file.txt output_file.csv

This method of running scripts is common and completely acceptable, but what happens when you plan to run a script on a regular basis? Without another method of running the script, you have to be available and remember to run the script manually on the command line. As you can imagine, this method isn’t optimal for scripts that should be run on a regular basis. In this situation, we need another method to regularly schedule scripts to run.

Both Windows and macOS have programs for running scripts and other executable files on a regular basis. Microsoft calls its program Task Scheduler; the program that does this on Unix and macOS is called cron (you may have heard of crontab files or cron jobs). This book has focused on running scripts on Windows, so the next section will demonstrate how to schedule a Python script to run regularly on Windows with Task Scheduler. At the same time, it is useful to know how to schedule cron jobs on macOS or Unix, so we will also demonstrate how to use cron to schedule a Python script to run regularly on those operating systems. ...

Get Foundations for Analytics with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.