Articles on: Dedicated Servers / VPS

How to run a Discord.py bot on a 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 <Folder/Directory> 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 <Startup Bot File> --interpreter=python3 or pm2 start <Startup Bot File> --name=<Name of the Process> --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 <Process ID or Name> 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 <Process ID or Name> 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 <Process ID or Name> or pm2 logs <Process ID or Name> --lines=<Line Amount> 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: 19/11/2022

Was this article helpful?

Share your feedback

Cancel

Thank you!