Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Using PM2

From Nest Wiki

Building a Discord/Slack bot? You should use PM2. PM2 is the easiest way to keep Node.js, Python, Bun, and other simple scripts running in the background. (If you're running a compiled backend service or something system-level, you should check out our guide on Using Systemd; it's likely a better fit!)

Note: "My-bot" is used as a filler in commands. You should change this before running commands.

1. Installation

First, update your container and install both Node.js and npm using this command:

apt update && apt install nodejs npm -y

Now, install PM2:

npm install -g pm2

2. Start Your Bot

Navigate to your project folder, then use the following commands to start your app, based on which language you're using.

Node.js:

pm2 start index.js --name "my-bot"

Python:

pm2 start app.py --name "my-bot" --interpreter python3

Bun:

pm2 start "bun run index.ts" --name "my-bot"

3. Make it Survive Reboots

When you inevitably need to restart or your LXC goes down, you want your bot to come back online without having to manually start it again. Run this command to make it survive reboots:

pm2 startup && pm2 save

Now you should be good to go!

4. Additional/Useful Commands:

Listing all apps:

pm2 list

Checking logs:

pm2 logs my-bot

Restarting a bot/app:

pm2 restart my-bot

Stopping a bot/app:

pm2 stop my-bot

Viewing logs (this command shows the last 100 lines):

pm2 logs my-bot --lines 100