Run n8n as a Service with PM2

Introduction

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.

Installing PM2

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:

  1. Log into a terminal or SSH session to your n8n server.
  2. 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:
PM2
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

pm2 list

Display PM2 Logs

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.

Terminal Dashboard

To launch the terminal dashboard, use the following command:

pm2 monit

This will give you four boxes on your screen with information about your processes, logs, customized metrics and application metadata.

pm2.io Website

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:

  1. Go to pm2.io
  2. Create an account
  3. Link your PM2 manager on your server to your pm2.io account
  4. Enjoy all the pretty graphs

For more details on how to do this, check out the web dashboard quick start documentation.

References

Some other useful documentation that may assist with your configuration:

Cheat Sheet

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

Conclusion

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!

7 Likes

Thephlon, Great i try i on weekend. Thanks.,

@Stefan Let me know if you run into any issues and I’ll try to be of assistance.

1 Like

@Tephlon

Very valuable content :slight_smile:
Thank you for sharing this, made my Setup much more stable, and thats all that matters!

2 Likes