How to Run Python Scripts on a VPS Server Long-Term

Many of us purchase VPS servers primarily for the purpose of hosting websites or providing network services. Sometimes, our servers may have idle resources after running certain services, and we may want to make use of these idle VPS servers.

So, how can we effectively utilize an idle VPS server? The most common approach is to run scripts that provide specific network services, such as running Python web crawlers on the server to fetch required information resources.

Now, the question is: How can we make these Python scripts run automatically and continuously? That’s what we’re going to discuss today.

As we all know, Python is an extremely useful tool for web development, data processing, and automation tasks.

In this article, we will explore how to run Python scripts on a Virtual Private Server (VPS) and ensure their long-term stability. The following instructions will involve Linux command-line operations, setting up the Python environment, and using the ‘screen’ command.

For this tutorial, we will use the Ubuntu environment as an example.

Uploading Python Scripts to the VPS Server

First, we need to upload our local Python script to the VPS server. If you have already created the script directly on the VPS server, you can skip this step. We can achieve this by using the SSH (Secure Shell) and SCP (Secure Copy) protocols. In the command-line interface, you can use the following command to upload the script to the VPS:

scp /path/to/your_script.py your_username@your_vps_ip:/path/on/vps

This command will copy your Python script from your local machine to the VPS server.

Installing Python and Required Libraries on the VPS

Next, we need to ensure that Python is installed on the VPS server. If it is not already installed, we can use the following commands to install Python 3 on an Ubuntu system:

sudo apt-get update
sudo apt-get install python3 python3-pip

Then, we need to install the libraries required by the Python script. One convenient approach is to create a ‘requirements.txt‘ file that lists all the required libraries and their versions. We can use the following command to install them:

pip3 install -r requirements.txt

This command automatically reads the information from the ‘requirements.txt‘ file and downloads and installs all the required libraries.

With this, we have set up the runtime environment for our Python server script.

Running Scripts Using nohup or screen

When we connect to a server via SSH, if we disconnect before a program finishes running, the program will stop. This is certainly not what we want. How can we solve this problem?

If we need to run a Python script continuously on a VPS server, even if the SSH connection is disconnected, we can use the ‘nohup‘ or ‘screen‘ command to achieve this.

Both of these commands allow programs to continue running in the background even after the SSH connection is disconnected. Here’s how to use them:

Using nohup:

nohup python3 /path/to/your_script.py

Or using screen:

First, enter the ‘screen‘ command and press Enter. Then execute the following command to run the script:

python3 /path/to/your_script.py

Press Ctrl+A and then press D to detach your Python script and run it in a new screen session. You can then disconnect the SSH connection.

How to use the screen command

Screen is a commonly used full-screen window manager in Linux that allows you to run multiple virtual terminals on a single physical terminal.

When your session is disconnected due to a network issue, screen allows you to keep your program running. When you reconnect, you can continue your work.

Here are some basic screen commands:

Start a new screen session: screen -S sessionname
Detach from a screen session: Ctrl+A D
Resume a screen session: screen -r sessionname_or_id
Terminate a screen session: Enter ‘exit’ in the screen session or press Ctrl+D
Switch between windows in a screen session: Ctrl+A N (next) or P (previous)
Create a new window: Ctrl+A C

Setting up Script Auto-Start on Boot

If you want your Python script to automatically run when the VPS starts up, you can add your startup command to the ‘/etc/rc.local’ file.

Leave a Comment