PM2
PM2 is a process manager. It manages your applications' states, so you can start, stop, restart, and delete processes. It offers features such as monitoring running processes and setting up a "start with operating system" (be that Windows, Linux, or Mac) so your processes start when you boot your system.
Installation
You can install PM2 via the following command:
npm install --global pm2Starting your app
After you install PM2, the easiest way you can start your app is by going to the directory your bot is in and then run the following:
pm2 start your-app-name.jsAdditional notes
The pm2 start script allows for more optional command-line arguments.
--name: This allows you to set the name of your process when listing it up withpm2 listorpm2 monit:
pm2 start your-app-name.js --name "Some cool name"--watch: This option will automatically restart your process as soon as a file change is detected, which can be useful for development environments:
pm2 start your-app-name.js --watchThe pm2 start command can take more optional parameters, but only these two are relevant. If you want to see all the
parameters available, you can check the documentation of pm2
here.
Once the process launches with pm2, you can run pm2 monit to monitor all console outputs from the processes started by pm2. This accounts for any console.log() in your code or outputted errors.
In a similar fashion to how you start the process, running pm2 stop will stop the current process without removing it from PM2's interface:
pm2 stop your-app-name.jsSetting up booting with your system
Perhaps one of the more useful features of PM2 is being able to boot up with your Operating System. This feature will ensure that your bot processes will always be started after an (unexpected) reboot (e.g., after a power outage).
The initial steps differ per OS. In this guide, we'll cover those for Windows and Linux/macOS.
Initial steps for Windows
It is recommended to use pm2-installer. Follow the steps over at their GitHub.
Initial steps for Linux/macOS
You'll need a start script, which you can get by running the following command:
# Detects the available init system, generates the config, and enables startup system
pm2 startupOr, if you want to specify your machine manually, select one of the options with the command:
pm2 startup [ubuntu | ubuntu14 | ubuntu12 | centos | centos6 | arch | oracle | amazon | macos | darwin | freesd | systemd | systemv | upstart | launchd | rcd | openrc]The output of running one of the commands listed above will output a command for you to run with all environment variables and options configured.
Example output for an Ubuntu user:
[PM2] You have to run this command as root. Execute the following command:
sudo su -c "env PATH=$PATH:/home/user/.nvm/versions/node/v8.9/bin pm2 startup ubuntu -u user --hp /home/userAfter running that command, you can continue to the next step.
Saving the current process list
To save the current process list so it will automatically get started after a restart, run the following command:
pm2 saveTo disable this, you can run the following command:
pm2 unstartup