Running a Bot in the Background
To make a script written in Python or any other programming language run stably and reliably on a server, you need to set up automatic startup and restart.
Preparation
1. First, upload the bot files to the server, for example to the /home/me directory. Then create a virtual environment with the following command:
python3 -m venv venv
2. Activate the virtual environment:
source ./venv/bin/activate
3. Install all the required packages listed in the requirements.txt file:
pip install -r requirements.txt
If there is no requirements.txt file with a list of packages, you can install the required packages manually.
Screen — the Simple Method
To simply run a script in the background, you can use the screen utility, which lets you create an unlimited number of terminal sessions.
First, install the screen utility from the repository:
sudo apt install screen
Then start a new session with the command:
screen
Activate the virtual environment and start the bot with the following commands:
source ./venv/bin/activate
python main.py
You can now detach the current session by holding Ctrl + A and then pressing D.
Here are some basic commands for working with screen:
- screen — create a new session;
- screen -ls — list all sessions;
- screen -r <session_name> — open a specific session (if you have only one session, you can omit the name).
Inside a session:
- Ctrl + A + D — detach the session;
- Ctrl + A + K — kill the session.
This method is quite easy to set up and does not require creating additional configuration files. However, if the server reboots or the script fails, you will have to restart the bot manually.
Systemd
Let's look at running your application as a service with Systemd, the Linux system and service manager that efficiently manages and starts services defined in configuration files.
First, create a new systemd unit with the following command:
sudo nano /lib/systemd/system/bot.service
Add the following content to the new unit:
[Unit]
Description=Bot service
After=network.target
[Service]
Type=idle
Restart=always
RestartSec=3
User=root
WorkingDirectory=/home/me/bot
ExecStart=/home/me/bot/venv/bin/python /home/me/bot/main.py
[Install]
WantedBy=multi-user.target
Where:
- Description is the service description,
- ExecStart specifies the path to the Python interpreter and the script,
- User defines the user the service runs as,
- Restart=always means the bot is restarted after an error.
Complete the setup by reloading the units, enabling autostart, and starting the bot:
sudo systemctl daemon-reload
sudo systemctl enable bot.service
sudo systemctl start bot.service
Now make sure the bot is working. If for some reason the bot does not respond, you can try running it in the terminal to identify possible errors.
PM2
If you prefer another way to run the bot, you can use the PM2 process manager. PM2 automatically restarts the bot if it fails and keeps its logs.
First, install the required Node.js and npm packages with the following commands:
sudo apt install nodejs
sudo apt install npm
Then install PM2 with the command:
npm install pm2 -g
To start your bot, go to its directory and run the following command:
pm2 start main.py --interpreter=python3
You now have two working options for running the bot: Systemd and PM2. Each has its own features and suits different use cases. Choose the one that fits your requirements and preferences.