DEV Community

Cover image for How to Schedule Python Scripts on a Mac Using Crontab
Luca Liu
Luca Liu

Posted on • Originally published at dev.to

How to Schedule Python Scripts on a Mac Using Crontab

Introduction

If you’re a Mac user and have ever wanted to run a Python script automatically at a specific time, MacOS has a built-in tool called crontab that lets you schedule tasks to run at specified intervals. This article will guide you through the steps to set up and use crontab to schedule your Python scripts.

Step 1: Edit the Crontab File

To edit the crontab file, run the following command in Terminal:



crontab -e


Enter fullscreen mode Exit fullscreen mode

Step 2: Schedule Your Python Script

In the crontab file, you’ll need to add a new line that specifies when and how often your script should run. The basic syntax for a crontab entry is:



* * * * * /path/to/python /path/to/your/script.py


Enter fullscreen mode Exit fullscreen mode

Here’s what each * represents (from left to right):

  1. Minute (0-59)
  2. Hour (0-23)
  3. Day of the month (1-31)
  4. Month (1-12)
  5. Day of the week (0-7, where 0 and 7 represent Sunday)

For example, if you want your script to run every day at 7:30 AM, you would write:



30 7 * * * /usr/bin/python3 /Users/yourusername/example.py


Enter fullscreen mode Exit fullscreen mode

Replace /usr/bin/python3 with the actual path to your Python interpreter from running which python3 in Terminal and /Users/yourusername/example.py with the path to your script.

It’s important to note that the paths don’t need to be enclosed in quotation marks.

Step 3: Save and Exit

After you’ve added the line to schedule your script, save the file and exit the editor. If you’re using the default editor, you can save your changes by pressing Ctrl + O, then hit Enter to confirm. Finally, press Ctrl + X to exit the editor.

Verifying Your Crontab Setup

If you’re new to crontab, a simple way to check if it’s working correctly is by creating a Python script that logs the current time every time it runs. Here’s how you can do it:

Step 1: Create a Simple Python Script

Create a script called log_test.py that writes the current date and time to a log file:



import datetime

# Define the log file path
log_file_path = "/path/to/your/log_file.txt"

# Get the current time
current_time = datetime.datetime.now()

# Write the current time to the log file
with open(log_file_path, "a") as log_file:
    log_file.write(f"Script ran at: {current_time}\n")


Enter fullscreen mode Exit fullscreen mode

Replace "/path/to/your/log_file.txt" with the path where you want the log file to be saved.

Step 2:  Test Your Script in the Terminal

Before scheduling the script with crontab, it’s important to ensure that it runs correctly in the terminal. Open the Terminal and execute the following command:



/usr/bin/python3 /path/to/your/log_test.py


Enter fullscreen mode Exit fullscreen mode

Replace /usr/bin/python3 with the path to your Python interpreter and /path/to/your/log_test.py with the path to your script. If the command runs without errors and you see a new entry in your log file, your script is ready to be scheduled.

Step 3: Update and Save Your Crontab

To schedule the script to run every minute, add this line to your crontab:



* * * * * /usr/bin/python3 /path/to/your/log_test.py


Enter fullscreen mode Exit fullscreen mode

Replace /usr/bin/python3 and /path/to/your/log_test.py with the correct paths on your system.

Step 4: Check the Log File

After a few minutes, check the log file. If you see new timestamps, your crontab job is working!

Conclusion

Congratulations! You’ve now set up a Python script to run automatically on your Mac using crontab. This is a powerful way to automate tasks, from running backups to generating reports. With crontab, you can ensure your Python scripts run exactly when you need them to, without any manual intervention.


Explore more

Thank you for taking the time to explore data-related insights with me. I appreciate your engagement.

🚀 Connect with me on LinkedIn

Top comments (0)