Hosting: VPS
A virtual private server, mostly known as VPS, is a virtual machine running in the cloud with its users having the full control of its system.
In this guide, you’ll learn about various methods of running your bot in a VPS, keeping it online 24/7, making it run automatically when your VPS boots and restart on crashes.
systemd
systemd is a powerful service manager which is pre-installed on many Linux distributions, mainly Debian-based ones.
Getting the Start Command
- Get the full path of your runtime:
# If using Deno
which deno
# If using Node.js
which node
You should have the full path of your entry file, too.
Your start command should look like the following:
<full_runtime_path> <options> <full_entry_file_path>
# Deno example:
# /home/user/.deno/bin/deno --allow-all /home/user/bot1/mod.ts
# Node.js example:
# /home/user/.nvm/versions/node/v16.9.1/bin/node /home/user/bot1/index.js
Creating the Service
- Go to the services directory:
cd /etc/systemd/system
- Open your new service file with an editor:
nano bot1.service
- Add the following content:
[Service]
ExecStart=<start_command>
Restart=on-failure
[Install]
WantedBy=multi-user.target
Replace
<start
with the command you got above._command>
- Reload systemd whenever you edit the service:
systemctl daemon-reload
Managing the Service
Start
systemctl start <service_name>
Replace
<service
with the file name of the service. Example:_name> systemctl start bot1
Run on Boot
systemctl enable <service_name>
Check Logs
systemctl status <service_name>
Restart
systemctl restart <service_name>
Stop
systemctl stop <service_name>
Don’t Run on Boot
systemctl disable <service_name>
PM2 (Node.js only)
PM2 is a daemon process manager for Node.js that will help you manage and keep your app online 24/7.
Installing
npm install pm2@latest -g
# If using Yarn
yarn global add pm2
Managing Apps
Start
pm2 start --name <app_name> <entry_point>
The
<app
can be any identifier to your app, for example:_name> bot1
. The<entry
should be the path to your index file (which runs your bot)._point>
Restart
By restarting, you stop the app, and start it again.
pm2 restart <app_name>
Reload
By reloading, you replace the current process of your app with a new one, resulting in a 0-second downtime. This is recommended for stateless applications.
pm2 reload <app_name>
Stop
# A single app
pm2 stop <app_name>
# All apps
pm2 stop all
Delete
By deleting, you stop your app and remove its logs and metrics.
pm2 del <app_name>
Advanced Information
For more, please refer to https://