In this guide on VPS administration, we will explain how to let a background application such as a discord bot, a nodejs application or a game server (minecraft, gmod, fivem, etc) run on supervisor on a Linux VPS under Debian , Ubuntu or CentOS.
This method allows you to keep an application running even if you disconnect from your VPS. Moreover, Supervisor offers additional features, to facilitate the management of processes, debugging with logs, and permit an automatic restart in case of crash.
Install Supervisor
Supervisor is an application for linux systems allowing to control a process. You must have administrator rights to enter the following commands.
To install it, we will use the package manager.
Installation of Supervisor on Debian
To install Supervisor on Debian :
apt update && apt install -y supervisor
Installation of Supervisor on Ubuntu
To install Supervisor on Ubuntu :
sudo apt update && sudo apt install -y supervisor
Installation of Supervisor on CentOS
To install Supervisor on CentOS :
yum install supervisor
Configuration of supervisord
Supervisord is the responsible part for starting and monitoring the configured applications. It will also restart these processes in case of a crash, if specified in the configuration. The general configuration is done in the file /etc/supervisor/supervisord.conf
.
Below you will find several configuration examples, for bot discord or game server.
To take into account any new configuration:
/etc/init.d/supervisor restart
Automatic reboot for a Discord Bot
To manage the automatic launch and restart of a discord bot, create a new file /etc/supervisor/conf.d/bot-discord.conf
with a configuration like this:
[program:discord_bot]
directory=/var/www/discord/
command=node index.js
autorestart=true
user=www-data
redirect_stderr=true
stdout_logfile=/var/www/discord/bot.log
stdout_logfile_maxbytes=50MB
startretries=3
startsecs=0
-
directory
contains the directory where your bot discord is. -
command
contains the command to launch the discord bot. -
autorestart
Ãtrue
for an automatic restart in case of a crash discord. -
user
contains the name of the system user who will start the process. -
redirect_stderr
ontrue
to get the bots errors in the log file. -
stdout_logfile
contains the path to the bot's log file, it will contain the feedback displayed by the bot. -
stdout_logfile_maxbytes
is the maximum size of the log file. -
startretries
indicates the number of error starts before theautorestart
is disabled. Prevents a looping restart in case of an error.
Automatic game server restart
To manage the automatic start and restart of a game server, create a new file
/etc/supervisor/conf.d/gameserver.conf
with a configuration like:
[program:gameserver]
directory=/home/minecraft/server/
command=java -jar minecraft_server.js
autorestart=true
user=minecraft
redirect_stderr=true
stdout_logfile=/home/minecraft/server/gameserver.log
stdout_logfile_maxbytes=100MB
startretries=3
startsecs=0
-
directory
contains the directory where your bot discord is. -
command
contains the command to launch the discord bot. -
autorestart
Ãtrue
for an automatic restart in case of a crash discord. -
user
contains the name of the system user who will start the process. -
redirect_stderr
ontrue
to get the bots errors in the log file. -
stdout_logfile
contains the path to the bot's log file, it will contain the feedback displayed by the bot. -
stdout_logfile_maxbytes
is the maximum size of the log file. -
startretries
indicates the number of error starts before theautorestart
is disabled. Prevents a looping restart in case of an error.
Supervisord commands
A multitude of commands are available, to see the complete list, type man supervisorctl
.
Start supervisord
/etc/init.d/supervisor start
Stop supervisord
/etc/init.d/supervisor stop
Restart supervisord
/etc/init.d/supervisor restart
Process status
To display the list of processes configured in supervisord and their status (started, stopped, crashed, etc):
supervisorctl status
Will return by example:
discord_bot RUNNING pid 6183, uptime 17:08:51
Start a process
supervisorctl start PROCESSNAME
Stop a process
supervisorctl stop PROCESSNAME
Restart a process
supervisorctl restart PROCESSNAME