Web based applications are wonderful tools that have a very low barrier to entry and an ease of use because, who doesn’t know how to use a web browser? And NodeJS does a wonderful job at creating these apps.
But, what if I want to run my NodeJS apps automatically as a service? Most of the time, NodeJS apps ask us to enter in start commands from the command line to get things going. What happens if I exit my terminal/SSH session? Or my server reboots?
This is where PM2 comes to the rescue. It allows you to wrap your NodeJS application into a service. You can now configure it to stop and start automatically and even restart in the event of a crash.
Assumptions and System Environment
This document assumes the following:
- You have a running copy of n8n on the system
- You have SSH/terminal access to the system
- You have either root/sudo/admin access to the system for application installation
This document was created using an Ubuntu 18.04 VM fully patched with the latest updates as of April 21, 2020. The system had dual CPU, 4 GB RAM, and 16 GB HDD.
The wonderful thing about PM2 is that it is a NodeJS application so if you are comfortable working with NodeJS applications (which I’m assuming you are since you are running n8n), then installing PM2 should be a snap! Simply follow these steps:
- Log into a terminal or SSH session to your n8n server.
- Run the following command:
npm install [email protected] -g
You should now have PM2 installed on your system.
Running n8n with PM2
In order to run n8n with PM2, enter the following command:
pm2 start n8n
You should end up getting a response something like this:
You will notice that I have more than one NodeJS application running with PM2.
Auto-starting n8n on Bootup
Another awesome aspect of running n8n like a service is that you can start it anytime your system reboots. That means that as soon as your system is back up and running, so is n8n.
Once you have n8n running with PM2, you can configure it to auto-start by executing the two following commands:
pm2 startup pm2 save
The first command will prompt you to run a command that generates the startup script.
The second command saves the startup script so that it is automatically executed the next time your system reboots.
Managing the n8n Service
Now that n8n is running and properly configured as an automatically running service, you should know how to perform some basic maintenance tasks with the service so that you can keep n8n running in tip top shape.
Stopping n8n Service
pm2 stop n8n
Restart n8n Service
pm2 restart n8n
Reload n8n Service
pm2 reload n8n
Remove n8n Service
pm2 delete n8n
List PM2 Services
Display PM2 Logs
You can also use the
pm2 logs --lines 100 command to just display the last 100 lines.
100 can be any number that you want it to be.
Monitoring n8n Service
There are two main ways that you can monitor the n8n service that pm2 is running; using the terminal dashboard or using the pm2.io website.
To launch the terminal dashboard, use the following command:
This will give you four boxes on your screen with information about your processes, logs, customized metrics and application metadata.
The pm2.io website gives you a complete web interface into what n8n is doing. In order to use it, you will need to do the following:
- Go to pm2.io
- Create an account
- Link your PM2 manager on your server to your pm2.io account
- Enjoy all the pretty graphs
For more details on how to do this, check out the web dashboard quick start documentation.
Some other useful documentation that may assist with your configuration:
Credit where credit is due. This cheat sheet is not my work. I essentially copy and pasted from here.
# Fork mode pm2 start n8n --name n8n # Name process # Cluster mode pm2 start n8n -i 0 # Will start maximum processes with LB depending on available CPUs pm2 start n8n -i max # Same as above, but deprecated. pm2 scale n8n +3 # Scales `app` up by 3 workers pm2 scale n8n 2 # Scales `app` up or down to 2 workers total # Listing pm2 list # Display all processes status pm2 jlist # Print process list in raw JSON pm2 prettylist # Print process list in beautified JSON pm2 describe 0 # Display all informations about a specific process pm2 monit # Monitor all processes # Logs pm2 logs [--raw] # Display all processes logs in streaming pm2 flush # Empty all log files pm2 reloadLogs # Reload all logs # Actions pm2 stop all # Stop all processes pm2 restart all # Restart all processes pm2 reload all # Will 0s downtime reload (for NETWORKED apps) pm2 stop 0 # Stop specific process id pm2 restart 0 # Restart specific process id pm2 delete 0 # Will remove process from pm2 list pm2 delete all # Will remove all processes from pm2 list # Misc pm2 reset <process> # Reset meta data (restarted time...) pm2 updatePM2 # Update in memory pm2 pm2 ping # Ensure pm2 daemon has been launched pm2 sendSignal SIGUSR2 n8n # Send system signal to script pm2 start n8n --no-daemon pm2 start n8n --no-vizion pm2 start n8n --no-autorestart
I hope that you have found this to be a useful tutorial. Let me know if you like this type of information and I’ll look into writing up some more!