PM2技术问题:Windows 2008 R2服务器多管理员用户无法执行命令
Hey there, let's tackle why User 2 can't run PM2 commands even though they're an admin like User 1. This is usually tied to npm global paths, environment variables, or service permissions—here's how to fix it step by step:
1. Verify Global npm Paths & Environment Variables
Windows often installs global npm packages in the current user's profile directory (e.g., C:\Users\User1\AppData\Roaming\npm), which other users can't access by default. Let's check:
- Have both users run this command in a command prompt:
npm config get prefix - If the paths differ (User 1's path is their own profile, User 2's is theirs), you have two options:
- Option A: Use a shared global path
- For both users, set npm to install global packages in a public admin-accessible directory (run as admin):
npm config set prefix "C:\Program Files\npm" - Add this path to the system-wide
PATHvariable:- Right-click "Computer" → "Properties" → "Advanced System Settings" → "Environment Variables"
- Under "System Variables", edit
PATHand addC:\Program Files\npmandC:\Program Files\npm\node_modules
- For both users, set npm to install global packages in a public admin-accessible directory (run as admin):
- Option B: Grant access to User 1's npm directory
- Navigate to User 1's npm path (e.g.,
C:\Users\User1\AppData\Roaming\npm), right-click → "Properties" → "Security" - Add User 2's account and grant "Full Control" permissions
- Navigate to User 1's npm path (e.g.,
- Option A: Use a shared global path
2. Check PM2 Startup Service Identity
If User 1 set up PM2 to start on boot with pm2 startup, the service is registered under their user account—so User 2 can't manage those processes. Fix this:
- Have User 2 open an elevated command prompt (right-click → "Run as administrator")
- First, remove the existing user-specific service:
pm2 unstartup - Then register PM2 as a system-wide service (accessible by all admins):
pm2 startup windows --user SYSTEM - Save the process list and restart the service:
pm2 save pm2 restart all
3. Ensure Elevated Permissions
Even as admins, Windows sometimes restricts access in non-elevated windows:
- Always have User 2 run command prompts as administrator when using PM2 commands. This avoids permission blocks on system-level resources.
4. Reinstall PM2 to the Shared Path
After setting up the shared global npm path, have User 2 run this in an elevated prompt to ensure PM2 is installed in the accessible directory:
npm install -g pm2
Once you've tried these steps, User 2 should be able to run pm2 list, pm2 start, and other commands just like User 1.
内容的提问来源于stack exchange,提问作者Leonardo Lima

