Skip to main content
RapidDev - Software Development Agency
n8n-tutorial

How to Run n8n as a Background Service

To run n8n as a background service, create a systemd unit file on Linux, use PM2 as a Node.js process manager, or deploy with Docker using a restart policy. All three methods ensure n8n starts automatically on boot, restarts after crashes, and runs without requiring an open terminal session.

What you'll learn

  • How to create a systemd unit file for n8n on Linux
  • How to use PM2 to manage n8n as a background process
  • How to run n8n in Docker with automatic restart policies
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Beginner7 min read15-20 minutesn8n 1.0+ (self-hosted on Linux, macOS, or Docker)March 2026RapidDev Engineering Team
TL;DR

To run n8n as a background service, create a systemd unit file on Linux, use PM2 as a Node.js process manager, or deploy with Docker using a restart policy. All three methods ensure n8n starts automatically on boot, restarts after crashes, and runs without requiring an open terminal session.

Running n8n Persistently as a Background Service

By default, running n8n from the command line ties it to your terminal session — if you close the terminal or SSH connection, n8n stops. For production use, you need n8n to run as a background service that starts on boot, survives disconnections, and restarts automatically after crashes. This tutorial covers three approaches: systemd on Linux, PM2 for any Node.js environment, and Docker with restart policies.

Prerequisites

  • n8n installed on your server (via npm, Docker, or binary)
  • Root or sudo access (for systemd)
  • Node.js 18+ and npm installed (for PM2 method)

Step-by-step guide

1

Option A: Create a systemd service file for n8n

Systemd is the standard service manager on most Linux distributions (Ubuntu, Debian, CentOS, RHEL). Create a service unit file that tells systemd how to start, stop, and restart n8n. The file defines the user to run n8n as, environment variables, and the restart policy. After creating the file, enable the service so it starts on boot, then start it immediately. This is the most robust option for Linux servers.

typescript
1# Create the service file
2sudo nano /etc/systemd/system/n8n.service
3
4# Paste the following content:
5[Unit]
6Description=n8n Workflow Automation
7After=network.target
8
9[Service]
10Type=simple
11User=n8n
12WorkingDirectory=/home/n8n
13ExecStart=/usr/bin/n8n start
14Restart=always
15RestartSec=10
16Environment=N8N_PORT=5678
17Environment=N8N_ENCRYPTION_KEY=your-encryption-key
18Environment=WEBHOOK_URL=https://n8n.yourdomain.com/
19Environment=GENERIC_TIMEZONE=America/New_York
20Environment=DB_TYPE=postgresdb
21Environment=DB_POSTGRESDB_HOST=localhost
22Environment=DB_POSTGRESDB_PORT=5432
23Environment=DB_POSTGRESDB_DATABASE=n8n
24Environment=DB_POSTGRESDB_USER=n8n
25Environment=DB_POSTGRESDB_PASSWORD=your-db-password
26
27[Install]
28WantedBy=multi-user.target

Expected result: The service file is created at /etc/systemd/system/n8n.service.

2

Enable and start the systemd service

After creating the service file, reload systemd to pick up the new configuration, enable the service to start on boot, and start it immediately. Verify the service is running by checking its status. The logs are available through journalctl. If the service fails to start, the logs will show the exact error message.

typescript
1# Reload systemd to recognize the new service
2sudo systemctl daemon-reload
3
4# Enable the service to start on boot
5sudo systemctl enable n8n
6
7# Start the service now
8sudo systemctl start n8n
9
10# Check the status
11sudo systemctl status n8n
12
13# View logs
14journalctl -u n8n -f

Expected result: The n8n service is active and running. It will automatically start on server reboot.

3

Option B: Use PM2 to manage n8n as a background process

PM2 is a production process manager for Node.js applications. It works on Linux and macOS and does not require root access. Install PM2 globally, then start n8n with PM2. Configure PM2 to start on boot with the startup command. PM2 automatically restarts n8n if it crashes, logs output to files, and provides a monitoring dashboard.

typescript
1# Install PM2 globally
2npm install -g pm2
3
4# Start n8n with PM2
5N8N_ENCRYPTION_KEY=your-encryption-key \
6WEBHOOK_URL=https://n8n.yourdomain.com/ \
7pm2 start n8n -- start
8
9# Or use an ecosystem file for complex configuration
10# (see complete_code section below)
11
12# Save the PM2 process list
13pm2 save
14
15# Generate startup script (run the command it outputs)
16pm2 startup
17
18# Check status
19pm2 status
20
21# View logs
22pm2 logs n8n

Expected result: n8n is running as a PM2-managed process that automatically restarts on crash and starts on boot.

4

Option C: Run n8n in Docker with a restart policy

Docker provides the simplest setup for running n8n as a background service. The --restart=always flag tells Docker to restart the n8n container if it crashes, and to start it when the Docker daemon starts (which is typically on boot). Use Docker Compose for easier configuration management. The container runs in detached mode (-d), so it does not require a terminal session.

typescript
1# Single Docker command
2docker run -d \
3 --name n8n \
4 --restart=always \
5 -p 5678:5678 \
6 -e N8N_ENCRYPTION_KEY=your-encryption-key \
7 -e WEBHOOK_URL=https://n8n.yourdomain.com/ \
8 -e GENERIC_TIMEZONE=America/New_York \
9 -v n8n_data:/home/node/.n8n \
10 n8nio/n8n
11
12# Check it is running
13docker ps | grep n8n
14
15# View logs
16docker logs n8n -f

Expected result: n8n runs in a Docker container that restarts automatically after crashes and on server reboot.

5

Verify the background service is working correctly

After setting up any of the three methods, verify that n8n is running by opening the web UI in your browser at http://your-server-ip:5678. Then test the auto-restart by stopping and starting the service or simulating a crash. For systemd, run sudo systemctl restart n8n. For PM2, run pm2 restart n8n. For Docker, run docker restart n8n. Check that n8n becomes available again within 10-30 seconds. Finally, test boot persistence by rebooting the server and confirming n8n starts automatically.

typescript
1# Test auto-restart (all methods)
2# Systemd
3sudo systemctl restart n8n && systemctl status n8n
4
5# PM2
6pm2 restart n8n && pm2 status
7
8# Docker
9docker restart n8n && docker ps | grep n8n
10
11# Test boot persistence (reboot and check)
12sudo reboot
13# After reboot, SSH back in and verify:
14systemctl status n8n # or
15pm2 status # or
16docker ps | grep n8n

Expected result: n8n restarts automatically and is accessible in the browser after both a restart and a full server reboot.

Complete working example

ecosystem.config.js
1// PM2 Ecosystem File for n8n
2// Save as ecosystem.config.js and run: pm2 start ecosystem.config.js
3
4module.exports = {
5 apps: [
6 {
7 name: 'n8n',
8 script: 'n8n',
9 args: 'start',
10 interpreter: 'none',
11 autorestart: true,
12 watch: false,
13 max_restarts: 10,
14 restart_delay: 5000,
15 max_memory_restart: '1G',
16 env: {
17 N8N_PORT: 5678,
18 N8N_ENCRYPTION_KEY: 'your-encryption-key',
19 WEBHOOK_URL: 'https://n8n.yourdomain.com/',
20 GENERIC_TIMEZONE: 'America/New_York',
21 N8N_LOG_LEVEL: 'info',
22 EXECUTIONS_DATA_PRUNE: 'true',
23 EXECUTIONS_DATA_MAX_AGE: '168',
24 EXECUTIONS_DATA_SAVE_ON_ERROR: 'all',
25 EXECUTIONS_DATA_SAVE_ON_SUCCESS: 'none',
26 DB_TYPE: 'postgresdb',
27 DB_POSTGRESDB_HOST: 'localhost',
28 DB_POSTGRESDB_PORT: '5432',
29 DB_POSTGRESDB_DATABASE: 'n8n',
30 DB_POSTGRESDB_USER: 'n8n',
31 DB_POSTGRESDB_PASSWORD: 'your-db-password'
32 },
33 env_production: {
34 NODE_ENV: 'production',
35 N8N_LOG_LEVEL: 'warn'
36 }
37 }
38 ]
39};
40
41// Usage:
42// pm2 start ecosystem.config.js
43// pm2 start ecosystem.config.js --env production
44// pm2 save
45// pm2 startup

Common mistakes when running n8n as a Background Service

Why it's a problem: Running n8n with nohup or screen instead of a proper service manager

How to avoid: Use systemd, PM2, or Docker. nohup and screen do not restart on crash or start on boot reliably.

Why it's a problem: Running n8n as root, creating security risks

How to avoid: Create a dedicated n8n user and run the service as that user. Set User=n8n in the systemd file.

Why it's a problem: Setting environment variables in .bashrc instead of the service file

How to avoid: Service managers do not read shell profiles. Set environment variables in the systemd unit file, PM2 ecosystem file, or Docker Compose file.

Why it's a problem: Forgetting to run pm2 save after starting n8n, causing PM2 to forget the process on reboot

How to avoid: Always run pm2 save after starting or updating PM2 processes, and run pm2 startup to generate the boot script.

Best practices

  • Always use a restart policy (Restart=always, --restart=always, or autorestart: true) to recover from crashes
  • Create a dedicated non-root user for running n8n to improve security
  • Store environment variables in the service configuration, not in shell profiles
  • Use PostgreSQL instead of SQLite for production deployments with multiple workflows
  • Set a memory limit (max_memory_restart in PM2 or Docker memory limits) to prevent n8n from consuming all server RAM
  • Configure log rotation to prevent log files from filling the disk
  • Monitor the service with health checks — curl the n8n health endpoint periodically

Still stuck?

Copy one of these prompts to get a personalized, step-by-step explanation.

ChatGPT Prompt

I installed n8n on an Ubuntu server with npm but it stops when I close my SSH session. How do I set it up as a systemd service that starts on boot, restarts on crash, and uses PostgreSQL?

n8n Prompt

Create a systemd unit file for n8n that runs as a dedicated user, connects to PostgreSQL, and restarts automatically. Include the commands to enable and start the service.

Frequently asked questions

Which method is best: systemd, PM2, or Docker?

Docker is the simplest to set up and the most portable. Systemd is the most native option for Linux servers. PM2 works well if you are already using Node.js and want a unified process manager. For production, Docker or systemd are recommended over PM2.

How do I check if n8n is running as a background service?

For systemd: systemctl status n8n. For PM2: pm2 status. For Docker: docker ps | grep n8n. Each command shows whether n8n is running, how long it has been up, and its process ID.

Can I run n8n as a background service on macOS?

macOS does not use systemd. Use PM2 or Docker instead. PM2 works natively on macOS and can be configured to start on login with pm2 startup. Docker Desktop for macOS also supports restart policies.

How do I update n8n when it is running as a service?

For npm installs: stop the service, run npm install -g n8n@latest, then start the service. For Docker: pull the latest image with docker pull n8nio/n8n, stop the container, and start it again. For Docker Compose: run docker compose pull && docker compose up -d.

What happens to running workflows when the service restarts?

Active workflow executions are interrupted. Scheduled triggers are re-registered after restart. If you have Save Execution Progress enabled, the partial data is available in the Executions tab.

Can RapidDev help me set up a production n8n deployment?

Yes, RapidDev can configure a production-grade n8n deployment with systemd or Docker, PostgreSQL, HTTPS, automated backups, and monitoring — ensuring your automation platform is reliable and maintainable.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.