You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Windows下Node服务器注销/重启后无需登录持续运行的方案

Fixing pm2-windows-service Stopping on User Logout

Great question—you’re absolutely on the right track here! The issue with pm2-windows-service halting when you log out is almost certainly linked to your user-specific PM2_HOME environment variable. Here’s why, and how to fix it:

Why the User-Specific Path Causes Problems

Windows services run under the Local System Account by default, not your personal user account. When you log out, your user profile directory (where a user-specific PM2_HOME is usually located, like C:\Users\YourName\.pm2) becomes restricted or unavailable to the system service. PM2 relies on this directory for process configurations, logs, and runtime data—so losing access to it causes the service to crash immediately.

Step-by-Step Fix: Switch to a System-Wide PM2_HOME

Follow these steps to move PM2_HOME to a universal, system-accessible path:

  1. Clean up your current PM2 setup

    • Stop all running PM2 processes:
      pm2 stop all
      
    • Delete all PM2 process entries:
      pm2 delete all
      
    • Uninstall the existing PM2 Windows service:
      pm2-service uninstall
      
  2. Set a system-wide PM2_HOME variable

    • Open Windows System Properties: Right-click "This PC" → "Properties" → "Advanced system settings" → "Environment Variables".
    • Under System variables (not User variables), click "New":
      • Variable name: PM2_HOME
      • Variable value: C:\ProgramData\pm2 (this is a standard system directory accessible to all accounts)
    • Click "OK" to save, then close all open terminals and reopen them to apply the new environment variable.
  3. Reinitialize PM2 and reinstall the service

    • Initialize PM2 with the new path:
      pm2 init
      
    • Start your application(s) as usual:
      pm2 start your-app.js # Replace with your app's actual entry file
      
    • Save the PM2 process list to persist across reboots:
      pm2 save
      
    • Install the PM2 Windows service again:
      pm2-service install
      
    • During installation, make sure to select the Local System Account when prompted. You can also check the box for "Allow service to interact with desktop" if you need to view PM2 logs in real-time (optional but helpful for debugging).
  4. Verify the fix

    • Restart your computer, then log out of your user account.
    • Log back in (or use remote desktop/Task Manager from another device) to check if your PM2 processes are still running. Confirm with:
      pm2 list
      

Extra Tips

  • If you had important logs or configurations in your old PM2_HOME directory, copy them to the new C:\ProgramData\pm2 folder before reinitializing PM2.
  • Ensure the new PM2_HOME directory has read/write permissions for the Local System Account (the default C:\ProgramData directory already has this, so no extra steps are needed unless you chose a custom path).

内容的提问来源于stack exchange,提问作者Naitik Adani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:44:42