Keep n8n running after closing terminal

Hi,

how do I keep n8n running after starting with “n8n start” and closing terminal?

1 Like

@LPilic welcome to the community.

You can do npm run dev & just add “&” after the command. This would work with macOS and Linux.

Thx,

that doesn’t work either:

Editor is now accessible via:
http://localhost:5678/

Press “o” to open in Browser.

Stopping n8n…
[1]+ Exit 254 npm run dev

I´m on Centos 7, created a supervisord .ini

My host is Uberspace.de. But as long as I keep Terminal open it’s running fine. Closing or exiting Terminal terminates the frontend/edito of n8n

Weird cuz it works for me but, I’m running macOS. So I doubt the issue is N8N related. @jan do you have any clue what maybe be happening here? You are better suited since you are a Linux user.

What you need is the same as for any Node.js application. You should probably check outpm2 or forever. That would however only solve the problem with the process not stopping and restarting it automatically if it crashes. Is probably best to follow the guide here and to run it with docker-compose:

That makes additionally also sure that you have SSL set up correctly which is manually also not that much fun.

Hi,

Thx. Unfortunately Docker is not an option. Will check out the other hint.

The GNU screen command may also be useful here. I’m running on an AWS EC2 and my n8n session is started within a screen, but put in the background. When I log on through SSH, I reattach the screen and can see what’s been happening with n8n logs. To log off, I use CTRL + A+ D and it switches back to another screen, where I log out of SSH.

I’m working on getting n8n to run with PM2 later today. I’ll post the configuration when it is ready.

4 Likes

As promised, I have created a brief tutorial on using PM2 to manage n8n as a service. Please check out Run n8n as a Service with PM2 for all the details.

4 Likes

Is it possible to get PM2 to load the configuration file too?

Hey @ljcn!

If you are referring to the .env file, then yes, this will actually load automatically.

Tephlon

1 Like

Thanks, that’s great!

Hey there,

know it’s been a while since this question was asked, but thought I’d give my solution since I just recently got n8n running, on a local ubuntu server.

You can run n8n off of systemd as a service by creating a n8n.service file in /lib/systemd/system/

example config:

[Unit]
Description=n8n
Documentation=https://n8n.io
After=network.target

[Service]
Environment=NODE_PORT=5678
Type=simple
User=<user_name_here>
ExecStart=/usr/bin/n8n
Restart=on-failure

[Install]
WantedBy=multi-user.target

do a daemon-reload:
sudo systemctl daemon-reload
then run with:
sudo systemctl start n8n
and finally, to start the service on boot:
sudo systemctl enable n8n

4 Likes

@Bernardo_Gamboa welcome to the community and thanks for sharing.

Thanks for this @Bernardo_Gamboa. Perhaps you can help me with a few questions:

  1. What username should go in User=<user_name_here>? My server username I assume (raspberry pi in this case)
  2. How can I add the --tunnel option?
  3. I’m running n8n using npm. And when I follow your instructions, the n8n service appears when I do a ps -aux | grep n8n but it doesn’t load in the browser. The ps -aux result also looks different between the npm version:
    admin 1053 101 15.6 558700 155652 pts/0 Sl+ 23:41 1:05 node /usr/bin/n8n start --tunnel
    and the systemctl service:
    admin 1104 125 7.2 185760 72184 ? Rsl 23:43 0:12 node /usr/bin/n8n

I’m no expert in this area, so am a bit lost… Thanks in advance.

Hey @theneilkirk,

If you are using the NPM option I would look at PM2 it makes the process easier and we have a handy guide here: How to set up n8n via PM2

To answer your questions though…

  1. The username is the OS user that you want to use to run the service as
  2. Where it has ExecStart=/usr/bin/n8n just change it to ExecStart=/usr/bin/n8n --tunnel that should do it
  3. It could be worth checking the OS log to see if anything has been outputted.
1 Like

Awesome. Thanks @Jon.

I’m still POCing my raspberry pi, so trying Docker and npm. Will try both options!

Hi again @Jon .

That guide is AMAZING! I now have PM2 running n8n at startup and on crash. Exactly what I was looking for.

Last piece, is that I can’t get the tunnel option working. In my PM2 config file I have listed it as an arg, which is I read from the PM2 docs:

module.exports = {
  apps : [{
    name   : "n8n",
    args:"--tunnel",
    env: {
      EXECUTIONS_DATA_PRUNE:true,
      EXECUTIONS_DATA_MAX_AGE=168,
      DB_SQLITE_VACUUM_ON_STARTUP=true,
      GENERIC_TIMEZONE:"Europe/London"
    },
    autorestart:true
  }]
}

But it doesn’t appear a tunnel is set up because my Telegram trigger says it isn’t… How should I pass that option with PM2?

Easiest method is the following:

  1. Start with: n8n < /dev/null (this will allow you to use Ctrl-Z to interrupt the process)
  2. Press Ctrl-Z (this will stop the process)
  3. Enter: bg (this will put the process in the background)
  4. Enter: disown (this will allow you to close the terminal and keep the background process alive)

Now you can close the terminal.

ps: if you want to stop the process, you have to find it:

  1. ps -ef | grep n8n
  2. use the second column number and do: kill <the_number> from 1)
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.