In a previous article, we talked about how to run Python from the Windows Task Scheduler. This post will cover how to schedule Python tasks on a Mac operating system as well as give an overview of the schedule package.
Using crontab on Mac
Python tasks can be scheduled on Mac using crontab. To do that, first, open up the Terminal. Then, we need to modify the crontab file. We can do that by typing crontab -e.
This will open crontab in the default editor, which is typically vim. You can change the editor by adding the editor name in front of our command – for example, to modify the crontab file using nano, we can run nano crontab -e (followed by enter).
Next, we need to add in a line describing the schedule frequency of how often we want the Python script to run. We input this in the order: minute, hour, day of the month, month, and day of the week. To leave one of these unspecified, place an asterisk (*) in that date / time slot.
Example 1: Run script on the first day of each month at 2:03.
3 2 1 * * python /path/to/test_script.py
Example 2: Run script every minute
* * * * * python /path/to/test_script.py
Example 3: Run script every hour at the 30th minute
30 * * * * python /path/to/test_script.py
Once you’ve scheduled your Python script, you need to save the crontab file. If you’re using nano, you can do that by typing ctrl+0 followed by ctrl+x to exit. In vim, you can save and exit by hitting esc and then typing :w, followed by enter.
Handling potential issues
One issue that may come up has to do with permissions with “full disk access”. This may happen if you’re using a recent version of macOS Mojave. For details on how to fix this issue, see here.
Another common issue that occurs is with path names. This is always good to check – for example, if you have multiple installations of Python installed, you’ll want to point to the correct installation needed for your scheduled job.
The schedule package
In addition to using crontab to run Python scripts, we can also use the schedule library for handling scheduling Python tasks. This package is especially useful for scheduling specific functions within Python applications. It also works across different operating systems. Let’s get started with the schedule package by installing it with pip:
pip install schedule
We’ll start by creating a simple function called test that simply prints out a message. Then, we can use schedule to schedule a task running this function every 10 seconds. To kick off this task, we can use the run_pending method, which will run inside of a while loop.
print("Test this out!")
The recurrence of the task can be adjusted by changing “seconds” to “minutes” or “hours” like below.
# every 10 minutes
# every 10 hours
Likewise, we can change the time unit length from 10 to any other number.
# every 1 minute
# every 30 seconds
Listing and clearing tasks
You can print out a list of the scheduled tasks by running schedule.jobs:
To clear out all tasks from the scheduler, we can run the following line of code:
Scheduling Python tasks from the Windows Task Scheduler
If you’re running on Windows, you can also schedule tasks via the built-in Task Scheduler. Learn more about how to do that by clicking here.
That covers it for this post. Check out other Python posts here.
Visit TheAutomatic.net for additional insight
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from TheAutomatic.net and is being posted with its permission. The views expressed in this material are solely those of the author and/or TheAutomatic.net and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.