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 pm2@latest -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!

15 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

@Tephlon Thanks for sharing.

I’m running n8n on windows server 2016. it doesn’t work with command pm2 start n8n as it constantly throw error “SyntaxError: Invalid or unexpected token”.

I found my answer from stackoverflow and uses this command.

pm2 start “C:\Program Files\nodejs\node_modules\n8n\dist\commands\start.js”

would be neat if there is a n8n.js.

1 Like

Thanks for the insight, @engowen. PM2 is specific to JavaScript so it makes sense that the CMD file does not run.

@engowen thanks for the way.

But everything just doesn’t work for me. On pm2, it constantly kills the process.

@Tephlon Can you tell me what to do?

Try getting it to work without the --tunnel option. I have heard some people mention that there are issues with that option.

Thanks for the answer. Trying without --tunnel was the first thing I did)) Did not work out.

Can you change your working directory to the location where you have n8n installed and then attempt to start it using the pm2 start n8n command?

Thanks.

I tried to run it from different places. With the pm2 start n8n command, the result is always the same as on the screenshot.

Is the issue fixed?

No, I haven’t found a solution.

After a little struggling, I was able to get PM2 to work on Windows Server 2016
Seems the trick is you need to cd to the appropriate start directory before running the start command

cd C:\Users\<User Profile>\AppData\Roaming\npm\node_modules\n8n\bin
pm2 start n8n
2 Likes

Incredible! Amazing! But it worked! Very grateful @MajesticPotatoe )

1 Like

No problem! Not sure why the native npm n8n.cmd runs has issues running but the one in the bin is fine (probably some issue with absolute/relative pathing).

You can also likely handle that in a ecosystem.config.js file by setting `cwd: ‘C:\Users<User Profile>\AppData\Roaming\npm\node_modules\n8n\bin’

Once you got it running though, pm2 save / pm2 resurrect becomes your best friend and you don’t have to worry about it so guess doesn’t matter much xD

2 Likes

This article was a good help to setup How to set up n8n via PM2
I also looked closer at environnement variables on Configuration | Docs to have a fine customisation.

I did successfully hosted n8n with https://caddyserver.com/ on debian and very light configuration as simple a that :

# /etc/caddy/Caddyfile
{myhost.io} {
    reverse_proxy localhost:5678
    log {
        output file /var/log/caddy/{myhost.io}.log
    }
}
1 Like

Hey @matyo91!

Welcome to the community :sparkling_heart:

I am glad you found the article to be helpful :slight_smile:

Also, thank you so much for sharing your solution!

1 Like

Thanks, @matyo91! I’m a huge fan of Caddy and I’m glad you got n8n running on it!

1 Like

Yes, so I did a PR for the documentation : 📝 Add caddy server configuration file by matyo91 · Pull Request #624 · n8n-io/n8n-docs · GitHub
See you in github for review.