Python

Make a python script run every hour on a Ubuntu VPS using crontab

Welcome back! Today’s blog is all about how to run a python script every minute or hour on a Ubuntu VPS using a great Ubuntu tool crontabs in 2022. Most developers works on windows and some of them don’t even know how to work with ubuntu and VPS, I was one of them and I was very struggling to find an answer for me that explains how to make my python script run every in a while on my Ubuntu VPS using crontab so I started digging and after so many errors, I got the answer so I’m going to write it down here to make you avoid doing all the long searching and dealing with all those unknown errors while deploying your python script on an Ubuntu virtual private server.

Note: In this article, I will be using my own python project which is an a.i that generates tweets and publish them, but for yourself, please use your own python project or script that you got.

To deploy your python project or script on ubuntu VPS, follow the steps below or watch this video.

What is Crontab/Cronjobs

What is a cronjob exactly? Imagine, you have a website that sends daily news to your subscribers on your emails. Now you have some news ready to send to them and you have scheduled them for today, tomorrow, the day after it, and so on. So now you have to go to your website and click on a button that sends the news scheduled for today to the subscribers. This is manual work, you have to go to your site and log in and do all the boring things, on top of that, you can’t be on exact time because you are human, but what if we have our server do those things automatically after a certain amount of time. Like you were sending your news at midnight every day, so you can make your server send the news of the day at midnight every day. To solve this problem, servers have a sort of scheduled tasks system, and those are called cronjobs. In simple words, cronjobs are scheduled tasks that are meant to be executed every minute/hour/day.

But what is crontab then? Crontab is actually cronjobs, or in other words, to make it simple you can say a crontab in Linux and macOS is a tool for cronjobs. To use crontab in Linux you can run crontab -e code to add and edit the cronjobs in your system.

Now that it’s clear what are cronjobs and how they work, let’s start making a python script run every in a while in Ubuntu VPS using cronjobs or you can say crontab.

Get a Ubuntu VPS

Of course, the first thing will be getting an Ubuntu VPS. There are many computing service providers on the market, but Hostinger has always been very fast and affordable for me. Plus Hostinger will provide you with 20% off if you use the link attached. Choose a plan that fits best to your python script or project and then create the VPS with a password. Make sure that you save the password somewhere safe because this is very important.

Connect with the VPS

As soon as you create the VPS, you have to install an SSH Client such as Bitvise or Putty to connect with the VPS. You can also watch the video above to get a graphical understanding of how it’s done.

Install Python and the packages

Now that we have the VPS and you have connected to that VPS, it’s time to install python and the dependencies of your script or project. For this example, I’m going to use my Twitter Bot which is on python. I’m going to install python first and the dependencies of my project.

Before we install any packages we will need to update the ubuntu package manager by running, apt update it on the terminal. After the updating of the package manager is done, now we need to install python, and to do that, we will run apt install python3 on the terminal, for some hosting providers such as Hostinger, python is already installed. After installing python, now let’s install the package manager of python, pip. Run apt install python3-pip.

After all these installations, we are ready to install the dependencies of our project. For my python project, I have two dependencies Tweepy and OpenAI, of course, it will be different for your project. So to install the dependencies we can run the usual python code, pip install [package-name] so for me, it will be pip install tweepy and pip install openai.

Transfer Python project to Ubuntu VPS

Now we are ready to run python scripts on our Ubuntu VPS, so now we can transfer our python project there. And to do that, we have to use the FTP protocol. Install any FTP application but I will recommend using FileZilla. So now you have to open your FTP application and enter the details of your VPS. The IP address of your VPS, the username, the password, and the port are to be 22. Here is a graphical example in FileZilla.

After you connect, go to the root folder of your VPS. And you should see something like the image below.

Go to the /home of your VPS, and create a new folder there. I’m going to name it Twitter.

And then just drag and drop the project files into that folder.

Okay after all that we are done with the project files, now let’s try to run the python script on our terminal and see if it works. Just make sure that you run it with python3 not only python. Me, I ran python3 twitter.py and I can see the A.I generated tweets on my Twitter account, so everything is working for my script. If you get any errors, make sure that you have all the dependencies installed. And check what the error is saying,

Now we are ready to make our python script run every hour on our Ubuntu VPS using crontab.

Run your python script using crontab in Ubuntu VPS

To run a python script every in a while, we use cronjobs. And for cronjobs, Ubuntu has a great tool called Crontab.

To get access to the crontab, you have to run sudo crontab -e the command, this will open the crontab file to be edited.

After running that command you will see a page like the image below.

After that, we are ready to add a new line to the file, a new line means a new cronjob task. A line will contain three parts separated by a space, the first being the time you want the task to repeat itself, the second being the program that is going to be used to run that script or task and then the path to the script that you are willing to run every in a while. Something like below.

* * * * * program-name /path/to/script.extension

The first five stars are the time, * which means, every minute, every hour, every day, and every month. The first * means the minute, which you can do 3 * * * * which will mean that you want to run the script every day of the month, at 00:03, if you do */3 * * * that will mean that you want to run the script after every 3 minutes. The second * is the hour, the third one is the day of the month, the fourth is the month, and the last one is the day of the week. I highly recommend checking crontab.guru awesome cronjob time calculator. It will help you understand how the crontab time system work and also you can use it to get the syntax of the time that you want.

The second part says program-name is the name of the program, for us it will be python3. And the third part of it is the path to your program.

Examples

Now let’s see what our line of crontab will look like if I want to run a python script every 5 minutes.

*/5 * * * * python3 /home/twitter/twitter.py

Let’s try to run a python script every hour.

0 * * * * python3 /home/twitter/twitter.py

What about if I want to run a python script every day?

0 0 * * * python3 /home/twitter/twitter.py

After you have made these changes, you can save the changes using CTRL + S and exit by CTRL + X. Now you have a python script that is scheduled to run every in a while using crontab in Ubuntu VPS.

Logging

You are getting errors on your cron job tasks and don’t know what’s wrong? Then the best option is to add another part to the cron job line to store the log into a file so that you can check what’s wrong.

To add a log file you have to add >> /path/to/logfile.log 2>&1

Thanks for reading this article, also read How to become a web developer without any degree and How to create an A. I powered Twitter bot.

Kachkol Asa

Share
Published by
Kachkol Asa

Recent Posts

5 Tips to prepare for a coding interview

Finally, you got a response from all the resume you submitted? Now It's time to…

7 months ago

5 Tips to improve your logic building in Programming

One of the hardest thing that beginners or even advanced coders is that they can't…

7 months ago

Complete Guide for Building a SaaS Platform using Laravel

Are you about to build a SaaS platform using Laravel? Well then you are at…

7 months ago

Coding for Dummies

If you are a programmer or a developer, then you might know what coding is.…

2 years ago

How to install export kit in Figma

If you are struggling with how to install export kit in Figma, then you are…

2 years ago

A Byte of Python Book: A must-read

If you are a regular reader of our blog, then there is no denying the…

2 years ago

This website uses cookies.