Skip to content

Deploying Software on Linux via PM2 Guide @ Linux Guide:

Note

PM2 only works with py (Python) and js (Javascript)!

Install Node.js and NPM:

For Ubuntu/Debian-based systems:

sudo apt-get install nodejs
For RPM-based systems (e.g., CentOS/RHEL):

sudo yum install nodejs
For Fedora-based systems:

sudo dnf install nodejs

How to install PM2?

$ npm install pm2@latest -g

OR:

$ yarn global add pm2

Starting Up:

*Ensure you are in the right directory when doing this command

$ pm2 start main.py 

You can add any of the following optional arguments to the start command:

# Specify an app name
--name <app_name>

# Watch and Restart app when files change
--watch

# Set memory threshold for app reload
--max-memory-restart <200MB>

# Specify log file
--log <log_path>

# Pass extra arguments to the script
-- arg1 arg2 arg3

# Delay between automatic restarts
--restart-delay <delay in ms>

# Prefix logs with time
--time

# Do not auto restart app
--no-autorestart

# Specify cron for forced restart
--cron <cron_pattern>

# Attach to application log
--no-daemon

Python Discord Bot Guide:

A simple guide on how to deploy a Python Discord Bot 24/7 with PM2.

  1. Transfer the Bot's Source Code into a new folder named discord-bot. Use FileZilla (Use Port Number 22 ) for file transfers between your PC/Laptop and your Bot's server.
    • You can overwrite files but not folders.
  2. Now use Putty to access your Bot's server's command line and do the rest of the steps below.
    • Use Port Number 22and "SSH" as your Connection Type.
  3. Enable Virtual Environments and Activate it.
    1. First cd to the Bot's source code's Folder. Example: cd discord-bot
    2. Then do the following commands in the Terminal: (This is to avoid Package Installation issues and more)
      1. python3 -m venv .venv
      2. source .venv/bin/activate
  4. Install all the nesccary packages. To do this please do the command below in the terminal/command-line.
    pip install -r requirements.txt 
    
  5. Install Node.JS
  6. Install PM2
  7. Do:
    pm2 start main.py --name=discord-bot --interpreter=python3
    
    Your bot should now be online. Please note PM2 will say "Online" even if your code has encountered syntax errors or etc.