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 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:
- Open your new service file with an editor:
- Add the following content:
[Service] ExecStart=<start_command> Restart=on-failure [Install] WantedBy=multi-user.target
<startwith the command you got above.
- Reload systemd whenever you edit the service:
Managing the Service
systemctl start <service_name>
<servicewith the file name of the service. Example:
systemctl start bot1
Run on Boot
systemctl enable <service_name>
systemctl status <service_name>
systemctl restart <service_name>
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.
npm install pm2@latest -g # If using Yarn yarn global add pm2
pm2 start --name <app_name> <entry_point>
<appcan be any identifier to your app, for example:
<entryshould be the path to your index file (which runs your bot).
By restarting, you stop the app, and start it again.
pm2 restart <app_name>
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>
# A single app pm2 stop <app_name> # All apps pm2 stop all
By deleting, you stop your app and remove its logs and metrics.
pm2 del <app_name>
For more, please refer to https://