How to run a Discord.py bot on a VPS

Articles on: Dedicated Servers / VPS


How to run a Discord.py bot on a VPS


  • Connect to your VPS via SSH

  • Run the following commands:

  • apt install python3-pip

  • apt install npm -y

  • npm i pm2 -g

  • After installing the packages, run the following commands:

  • pm2 flush

  • pm2 install pm2-logrotate

  • pm2 set pm2-logrotate:max_size 10M

  • pm2 set pm2-logrotate:compress true

  • pm2 set pm2-logrotate:rotateInterval '0 */1 * * *'

Running these commands will make it so that the pm2 logs stop creating very large log files in the long term.


How to start running Discord.PY Bots using PM2


  • Connect to your VPS via SFTP. In this case, we will be using FileZilla. Upload the contents of your bot to your VPS

  • Then using SSH, use the ls command to see the content of the folders that you are currently on, and use the cd ` command to navigate through the folders. Also, to go up a folder, run the command ` cd ../.

For Example: If you want to get from the “root” folder to “Discord Bots > DjsBot”, just run the command


cd ‘Discord Bots’/DjsBot


.

  • Now to start the bot, run the command pm2 start --interpreter=python3 or pm2 start --name= --interpreter=python3 to give the process a name.


How to restart the bot using PM2


  • To restart the bot, you need to get the bot’s process ID or the process name that you’ve set when starting the bot.

Note: You can get the process ID by doing


pm2 ls


and find the id corresponding to the process name

  • Run the command ` pm2 restart ` and your bot will restart.


How to stop the bot using PM2


  • To stop the bot, you need to get the bot’s process ID or the process name that you’ve set when starting the bot.

Note: You can get the process ID by doing


pm2 ls


and find the id corresponding to the process name

  • Run the command ` pm2 stop ` and your bot will restart.


How to view the logs of the bot using PM2


  • To view the logs of the bot, you need to get the bot’s process ID or the process name that you’ve set when starting the bot.

Note: You can get the process ID by doing


pm2 ls


and find the id corresponding to the process name

2.Run the command


` pm2 logs `


or


pm2 logs --lines=


and pm2 will show the bot’s console logs.

If you require any further assistance, please create a ticket here.

Created By: Alecz R.


Updated on: 07/03/2023

Actualizado el: 31/07/2024

¿Este artículo te resultó útil?

Comparte tu opinión

Cancelar

¡Gracias!