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
Option A: Create a systemd service file for n8n
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.
1# Create the service file2sudo nano /etc/systemd/system/n8n.service34# Paste the following content:5[Unit]6Description=n8n Workflow Automation7After=network.target89[Service]10Type=simple11User=n8n12WorkingDirectory=/home/n8n13ExecStart=/usr/bin/n8n start14Restart=always15RestartSec=1016Environment=N8N_PORT=567817Environment=N8N_ENCRYPTION_KEY=your-encryption-key18Environment=WEBHOOK_URL=https://n8n.yourdomain.com/19Environment=GENERIC_TIMEZONE=America/New_York20Environment=DB_TYPE=postgresdb21Environment=DB_POSTGRESDB_HOST=localhost22Environment=DB_POSTGRESDB_PORT=543223Environment=DB_POSTGRESDB_DATABASE=n8n24Environment=DB_POSTGRESDB_USER=n8n25Environment=DB_POSTGRESDB_PASSWORD=your-db-password2627[Install]28WantedBy=multi-user.targetExpected result: The service file is created at /etc/systemd/system/n8n.service.
Enable and start the systemd service
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.
1# Reload systemd to recognize the new service2sudo systemctl daemon-reload34# Enable the service to start on boot5sudo systemctl enable n8n67# Start the service now8sudo systemctl start n8n910# Check the status11sudo systemctl status n8n1213# View logs14journalctl -u n8n -fExpected result: The n8n service is active and running. It will automatically start on server reboot.
Option B: Use PM2 to manage n8n as a background process
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.
1# Install PM2 globally2npm install -g pm234# Start n8n with PM25N8N_ENCRYPTION_KEY=your-encryption-key \6WEBHOOK_URL=https://n8n.yourdomain.com/ \7pm2 start n8n -- start89# Or use an ecosystem file for complex configuration10# (see complete_code section below)1112# Save the PM2 process list13pm2 save1415# Generate startup script (run the command it outputs)16pm2 startup1718# Check status19pm2 status2021# View logs22pm2 logs n8nExpected result: n8n is running as a PM2-managed process that automatically restarts on crash and starts on boot.
Option C: Run n8n in Docker with a restart policy
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.
1# Single Docker command2docker 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/n8n1112# Check it is running13docker ps | grep n8n1415# View logs16docker logs n8n -fExpected result: n8n runs in a Docker container that restarts automatically after crashes and on server reboot.
Verify the background service is working correctly
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.
1# Test auto-restart (all methods)2# Systemd3sudo systemctl restart n8n && systemctl status n8n45# PM26pm2 restart n8n && pm2 status78# Docker9docker restart n8n && docker ps | grep n8n1011# Test boot persistence (reboot and check)12sudo reboot13# After reboot, SSH back in and verify:14systemctl status n8n # or15pm2 status # or16docker ps | grep n8nExpected result: n8n restarts automatically and is accessible in the browser after both a restart and a full server reboot.
Complete working example
1// PM2 Ecosystem File for n8n2// Save as ecosystem.config.js and run: pm2 start ecosystem.config.js34module.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};4041// Usage:42// pm2 start ecosystem.config.js43// pm2 start ecosystem.config.js --env production44// pm2 save45// pm2 startupCommon 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.
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?
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.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation