You’ve probably been using Python to automate a lot of tasks already. Maybe you’ve even set a task up to run your scripts locally using Windows Task Scheduler. But you can take automation a step further to the point that you don’t even have to be at your computer to run your scripts!
We’re going to walk through how to automatically run your Python scripts on a hosted web server using Cron Jobs.
This site is hosted on GreenGeeks and part of the reason I chose them is because they offer Python applications at all hosting levels. I have been nothing but impressed with their support team and I love that they’re environmentally friendly.
This step-by-step process is likely the same or very similar on other web hosts that use cPanel, but I cannot guarantee that.
Step 1: Ask Support for Shell Access
In order to keep your Python instance and modules up to date, you’re probably going to need shell access. If you’re only using built-in libraries, you can skip this step, but I’m guessing you aren’t. Shell access is available on all accounts, but it’s not activated by default. This is the first step simply because it can take some time for support to get back to you. Their support has been incredible for me and I usually get a response in less than 24 hours. My request was processed in 10 minutes.
We can continue the setup for now while we wait to hear back.
Step 2: Creating Python Virtual Environment
In your cPanel, scroll down to the Software section and click on Setup Python App. This will take you to a page with an empty list of web applications and a button to Create Application.
This is where I got a little confused to start. If you’re also confused, you’re in good company.
You don’t need a Python script ready to go already. By creating an application, we’re simply setting up a virtual environment – essentially think of it as installing Python on your server.
On the Creation page, we have a short form to fill out.
- Choose the Python version. At the time of writing this, only version 3.8.6 is available.
- Fill in the application root. This will be the directory path where your application will exist and where you’ll load your script files to run. This path does not need to exist already; it will be created. We used
pythonapplication
. - Fill in the public URL. This is a link to your application. Unless you’re publicly sharing your outputs, it doesn’t really matter what you put here. Again, we chose
pythonapplication
. - Name your application startup file. I just chose
app.py
. - And name your application entry point. I chose
app
. - (Optional) You can add environment variables at this time. You can also do this later, so I wouldn’t worry about it.
- Hit create at the top.
This will set up the app and take you to the application management for the new app you’ve created. We’ll come back to this, but before you leave this page, there is a section at the top that says, “Enter to the virtual environment. To enter to virtual environment, run the command: …” with a block of code. Click that code to copy it to your clipboard, then go back to the cPanel.
Step 3: Update pip and Install Libraries
Pause. In order to proceed, you must have Shell access to your account. Hopefully by now you have it.
There are two ways to go about accessing the terminal. The way I started out with was using PuTTY to create an SSH key, then connect through that. I’m not going to cover that process because it’s long and confusing and there’s a way easier way to access the terminal from the GreenGeeks cPanel.
Updating & Installing via Terminal
On the cPanel homepage, scroll down to the Advanced section and click on Terminal. This will take you to the shell.
You will be prompted with a warning. Accept the risk and move forward.
Now that you’re in the terminal, paste the block of code you copied from your Python application menu. You can right-click and paste or use Shift+Insert. Control+V does not work here.
This will change your directory to your virtual environment where you can update pip and install packages. For some reason, the default virtual environment is using pip version 9.0.1. This caused problems for me when installing, so I recommend updating pip first.
pip install --upgrade pip
At this point, you can either continue to install Python packages you know you’ll need or you can run a Pip Install from your Python app manager. I like to install via the terminal, but I also make the mistake of trying to keep my packages updated with scripts in production (you should really test updates first). So we’re going to walk through
Installing Requirements from Python App Manager
The first thing we need to do is create a requirements.txt file. Some IDEs can do this for you. If you’re not familiar with them, the short version is that they are a list of Python packages you use in your script(s) along with the versions. Go into your File Manager and find your Python application folder. If you’re following along with this exactly, it will be nested under your home folder and called pythonapplication
. Enter that folder and upload your requirements.txt file. And we’re done in the File Manager for now (though you could also skip to Step 4 and add your Python scripts here at the same time if you want). We’re going to jump between the File Manager and the Python App a couple of times, so you can leave it open in another tab.
Alright, so from the cPanel home, again, click on Setup Python App. Your application will appear momentarily and when it does, click on the edit button.
Scroll down until you find the Configuration files section. You’ll see a big blue button that says “Run Pip Install”, but you can’t click it yet. Just below that is a field where we will input “requirements.txt” and hit the Add button next to it. You can also edit the file by clicking on the edit button on the file you added below. You probably don’t need to do that now since you just uploaded it, but it’s good to know for the future.
Now, just click on the Run Pip Install button. It will run for a moment and, if everything went well, you’ll get a green pop-up saying PIP Install completed successfully.
Step 4: Add File(s) to Directory in File Manager
Back to the File Manager. We are ready to upload our Python script(s) to the pythonapplication
directory.
For this tutorial, I’m creating two scripts to verify they run and reliably run via automation.
The first script is hello_world.py
and is below. Replace {username}
with your username.
with open("itworks.txt", 'w') as file:
file.write('hello world. This works')
The second script is append_datetime.py
and is below. Replace {username}
with your username.
from datetime import datetime
with open("itworks.txt", 'a') as file:
file.write(f'\nThis file was updated at {datetime.now()}')
There’s nothing else we need to do here. It’s a pretty straightforward step.
Step 5: Test That Script Runs
Back to the Setup Python App. Just below where we ran the PIP Install, there is an Execute python script section. This is where we are going to test that both of the scripts we uploaded run properly. All we have to do is enter the path to the script file.
Note that because the application is housed in the pythonapplication
folder, we can start a relative path from there. In our case, we can just type in hello_world.py
and then hit the Run Script button.
Again, if you’re doing it right, you should get a green pop-up indicating success and there will also be a terminal output that appears below with any return codes.
I also want to test append_datetime.py
, so I’ll type that in and hit Run Script. Same result.
It said it worked, but did it do what we wanted? Let’s go back to the File Manager and reload the folder. We should now see a new file, itworks.txt
. If we open that, we should see that both scripts executed based on the two lines of text written!
Step 6: Create a CRON Job
We’re almost done! Our server is set up to run the Python script and all we have left is to automate it!
We’ll be using the server’s Cron Jobs. From the cPanel homepage, navigate to the Advanced section and click on Cron Jobs.
Cron Jobs are Linux commands that take a couple of arguments. The first set of arguments are to set the frequency of the run. They are formatted the following way: (minute hour day-of-month month day-of-week)
. I have a script I want to run every Sunday at 9am, for example. The time I set my cron to will be:
(0 9 * * 0)
Frankly, I use a tool for this. My favorite it the crontab guru by Cronitor.
The second thing we need is the command to run. I break this command down into three parts, separated by spaces.
First, we will change the directory since we are writing files with relative addresses.
cd /home/{username}/pythonapplication
Second, but at the same time, we are going to specify the location of our Python instance so the system knows we are running a Python script (as opposed to PHP, for example).
&& /home/{username}/virtualenv/pythonapplication/3.8/bin/python
Finally, we are calling the script that exists within our directory we changed. Since we’re already in the directory, this is just the name of the file.
append_datetime.py
So my full command looks like this:
cd /home/{username}/pythonapplication && /home/{username}/virtualenv/pythonapplication/3.8/bin/python append_datetime.py
In the screenshot below, I wanted to test this script every minute to make sure it was working properly, which means the cron settings were (* * * * *).
You did it! Congratulations! You’ve now automated your automation and it will run daily, weekly, monthly, or whenever you set your cron job to run.
Eric is a Python SEO with a passion for data. He uses Python to automate tasks and analyze large data sets. He is often the go-to for spreadsheet challenges, too. He sees every challenge as an opportunity to learn.