Cron system and crontab is the one in the background in your local Macbook. Every time you open the P&L calculator on Google Sheets, the Amazon competitor product pricing data is up-to-date. So you can instantly understand if your product pricing is competitive or not in the search result or somewhere else.
In this Python Tutorial, I would walk you through how to use Crontab to automate the Python scripts. By the end of this Python Tutorial, you can learn what Crontab is. And you can pick up what commands you can use. Of course you can master how to install and create the automatic execution with the help of this bot. It would help you learn my next Python Tutorial regarding server deployment as well.
- What’s Crontab and How it works with your Python Scripts
- Use Crontab Guru Schedule Stimulator
- Grant Cron and Terminal Full Disk Access in Macbook
- Create a Crontab Schedule in Macbook Using
- Check the Crontab Status Using
Python Tutorial – What’s Crontab and How It Works with your Python Scripts
Cron is a computer daemon. You can use it to execute scheduled and desired tasks in your computer background. It can be a one-off assignment or a recurring task.
A crontab file is just a simple text file. It contains a list of commands, and these commands aim to activate and run at specified times. It has a set of crontab commands, and the cron daemon checks the commands in the crontab file and executes them in the system background.
In Macbook, you can open the terminal and enter the command
crontab -e. You can add the crontab commands to automate your Python scripts stored in your Macbook. It means as long as your laptop is open and connected to the internet, the Python scripts can work automatically in the background without any manuals.
For example, previously I shared how to create an Amazon price tracker. Basically, this tracker needs to update at a designated time slot, otherwise, you can’t see the up-to-date market value. However, it’s not real to update manually by yourself. So, crontab can be your personal assistant.
Python Tutorial – Crontab Guru Schedule Stimulator
First thing first, we need to learn how to set the timer by using Crontab. After all, this is the primary feature of Crontab. There is a stimulator Crontab Guru, and you can search in Google and go to have a look at the simulator
By default, there are 5 variables of the timer setting. They are the minute, hour, day of the month, month, and day of the week. Apart from the number you can use, there are some signals you can use as well. They represent some commands. For example,
* represents any value. As you can see in the screenshot below, there is just the number 5 in the minute variable, and the rest of the variables are
*. That means we need to run the Python scripts every 5 mins.
For more details, please check out the simulator and try to play around.
Grant Cron and Terminal Full Disk Access in Macbook
You might be aware, Crontab seems to not work on Macbook after you tried to run the Python scripts in the terminal. In fact, it’s dealing with the permission issue, because since MacCatalina, we need to do further steps to grant Cron permission access. Below are the steps
- Open preferences and go to
“Security & Privacy” -> “Privacy”
- Scroll down to
“Full Disk Access”on the left side and select it.
- Hit the unlock icon and enter your password
- Hit the
“+”sign to add an application
“command+shift+G”to bring up the got to file dialog box
- Enter the full pathname to the application, that is
/usr/sbin/cron and click on “Go”
“Open”It should show up in the list with a checkmark next to it. I tried it with a simple csh script in cron and it worked.
What’s more, we also need to grant Terminal to Full disk as well, and the steps would be similar to the Cron steps.
Create a Crontab Schedule in Macbook Using Crontab -e
Now you might master how to set the timer, you can use the timer setting on the Crontab commands in Macbook Terminal. Basically, the crontab creation formula is the timer plus the application plus the Python Script
* * * * * + Path of Python3 application + Python script
First thing first, you need to copy your existing Python3 application location path. Crontab can’t detect your current shell applications automatically. You need to tell it which one you are going to use and open the scripts.
You can enter
“which python3” or
"which python" in the terminal and copy this path in advance. You would use this path later on.
Secondly, please go to your Python script folder and copy the file location and file name. You would also use it after the Python application path. It can tell Python which script you are going to run.
Last but not least, you can enter
crontab -e and go into the editor environment. Press
"I" to start the editor mode. Then, you can fill in the timer setting, python application path, and the script location and name. Please keep in mind that crontab doesn’t allow any space to exist in the application path and file path. Being said that, each section needs a space to separate.
Then you can press
“ESC”, and type in
“:wq” to exit the editor mode. Here has a stable of commands you can use in the vi editor.
Check the Crontab Status Using
Crontab - l,
mail and Delete the Crontab Demands Using
crontab -l in the terminal can show you the crontab automation commands you set up just now.
In some cases, you might come across you can’t create the commands by popping up this message in the terminal
“/usr/bin/vi exited with status 1”. The reason for this is because your system hasn’t installed the vi editor yet.
The solution is just to type in
“export EDITOR=vim” in the terminal, and then fill in
crontab -e to restart the editing
What’s more, in the terminal interface after coming out from the crontab editor, Crontab will not update you if the automation is working or not. On the other hand, you need to check the messages from the Cron system.
It’s very simple by just typing in
“mail” in the terminal, and you can get back a list of emails. Entering the number of the mail, you can open the message and understand the automation result. If your commands have any problems, you can learn from the message.
I hope you enjoy reading Python Tutorial 18: Crontab Automates Running Amazon Competitor Price Tracker for Updating P&L Calculator and Product Market Value. If you did, please support us by doing one of the things listed below, because it always helps out our channel.
- Support my channel through PayPal (paypal.me/Easy2digital)
- Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
- Follow and like my page Easy2Digital Facebook page
- Share the article to your social network with the hashtag #easy2digital
- Buy products with Easy2Digital 10% OFF Discount code (Easy2DigitalNewBuyers2021)
- You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software
- Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)