Running and Automating a Python Application on a Linux Server in 7 Easy Steps

Running and Automating a Python Application on a Linux Server in 7 Easy Steps

In this post I am going to tell the steps to running and automating a python application on a linux server in detail. We will use Linode for this article, but you can use Digital Ocean or Vultr, too. We will choose Debian Linux Server on Linode, place our Python file to the server, sync it with Github and automate the Python application using Crontab.

First, ssh to the Linode server:


Check where you are:

Login with root and check your system

It is a good idea to update your operating system and various packages to the latest version:

apt-get update
Update the operating system

We will now install pip3 and other required Python packages:

apt-get install python3-pip

We want to install git:

apt-get install git
install git on debian linux
git clone <your_git_repo_link>

If it is a private repo, Github wants to login with a personal authentication token. You can get the token in Github. After getting it, you need to configure local git client:

git config --global ""
git config --global ""
git config -l

Now you can clone your private repository using :

git clone <link to your private git repository> 

When asked username and password, just enter your github username. The important part is that you enter the personal access token as password!

If you want your server or local computer to remember the token:

git config --global credential.helper cache

And if you want to remove it, simply enter:

git config --global --unset credential.helper

After we clone the Git repository to the Linux Server, we can run the application.

pwd #where are we?
ls -al #list all the files and folders
cd <directory which contains our application>
ls -al #again list all the files and folders
python3 <your python application> #run your .py file

Make your changes in local VS Code setup and push your changes to the Github Repo. Than we pull the files to the Linux Server from the Github repo.

In the local computer:

git add .
git commit -m "new commit"
git push origin main

In the Linux Server:

cd <directory which contains our application>
git pull origin main
crontab -e

This opens a cron file in nano.

The following code runs your Python code every 2 hours and logs the output to an alert.log file. The trailing 2>&1 means that both stdout (standart output) and stderr (error output) goes to the same alert.log file.

0 */2 * * * /usr/bin/python3 /root/linode_private/ >> /root/linode_private/alert.log 2>&1

That’s all. These are the steps to running and automating a Python application on a linux server.