#!/bin/bash set -e # 导入环境变量 source /home/pn/.env current_time=$(date +"%Y-%m-%d %H:%M:%S") echo "Starting n8n at $current_time" # 输出数据库配置信息 echo "Database Configuration:" echo "Host: ${DB_POSTGRESDB_HOST}" echo "Port: ${DB_POSTGRESDB_PORT}" echo "User: ${DB_POSTGRESDB_USER}" echo "Database: ${DB_POSTGRESDB_DATABASE}" echo "Type: ${DB_TYPE}" # 等待 PostgreSQL 就绪, 并输出连接信息,超时10秒 wait_for_postgres() { echo "Waiting for PostgreSQL to start..." while ! nc -z ${DB_POSTGRESDB_HOST} ${DB_POSTGRESDB_PORT}; do echo "Trying to connect to PostgreSQL at ${DB_POSTGRESDB_HOST}:${DB_POSTGRESDB_PORT}..." sleep 1 done echo "PostgreSQL started" } # 启动 Redis 服务 start_redis() { echo "Starting Redis server..." redis-server --daemonize yes sleep 1 if redis-cli ping > /dev/null 2>&1; then echo "Redis server started successfully" else echo "Failed to start Redis server" exit 1 fi } wait_for_postgres start_redis # 设置 N8N 环境变量 export N8N_ENCRYPTION_KEY="${N8N_ENCRYPTION_KEY:-n8n8n8n}" # 允许使用所有内建模块 export NODE_FUNCTION_ALLOW_BUILTIN=* # 允许使用外部 npm 模块 export NODE_FUNCTION_ALLOW_EXTERNAL=* # Activate automatic data pruning export EXECUTIONS_DATA_PRUNE=true # Number of hours after execution that n8n deletes data export EXECUTIONS_DATA_MAX_AGE=36 # Number of executions to store export EXECUTIONS_DATA_PRUNE_MAX_COUNT=1000 # Save executions ending in errors export EXECUTIONS_DATA_SAVE_ON_ERROR=all # Save successful executions export EXECUTIONS_DATA_SAVE_ON_SUCCESS=all # Don't save node progress for each execution export EXECUTIONS_DATA_SAVE_ON_PROGRESS=false # Don't save manually launched executions export EXECUTIONS_DATA_SAVE_MANUAL_EXECUTIONS=false # 将以下环境变量设置为 false,以阻止 n8n 连接到其服务器 export N8N_DIAGNOSTICS_ENABLED=false export N8N_VERSION_NOTIFICATIONS_ENABLED=true export N8N_TEMPLATES_ENABLED=false # 设置文件权限 export N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true echo "Starting n8n with the following configuration:" echo "N8N_HOST: ${N8N_HOST}" echo "N8N_PORT: ${N8N_PORT}" echo "N8N_PROTOCOL: ${N8N_PROTOCOL}" echo "WEBHOOK_URL: ${WEBHOOK_URL}" echo "QUEUE_BULL_REDIS_HOST: ${QUEUE_BULL_REDIS_HOST}" echo "QUEUE_BULL_REDIS_PORT: ${QUEUE_BULL_REDIS_PORT}" echo "EXECUTIONS_MODE: ${EXECUTIONS_MODE}" # 使用绝对路径调用 n8n,添加日志输出 exec n8n start