Fix PM2 user permission error (UID 1002 not found)
All checks were successful
Cloud Node Container / Build-and-Push (18) (push) Successful in 1m58s
Cloud Node Container / Build-and-Push (20) (push) Successful in 1m51s
Cloud Node Container / Build-and-Push (22) (push) Successful in 1m53s

- Add proper error handling for user creation with detailed logging
- Verify user exists before starting PM2
- Set HOME environment variable when running PM2
- Run PM2 with --no-daemon flag and proper user context
- Add ownership fix for generated ecosystem.config.js

This should resolve the "User identifier does not exist: 1002" error
by ensuring the user is properly created and PM2 runs in the correct context.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-07-24 09:22:43 -07:00
parent 88d0d4e64b
commit 205c1c4d68

View File

@@ -8,7 +8,16 @@ if [ -z "$environment" ]; then
environment="PROD" environment="PROD"
fi fi
adduser -u $uid $user # Create user with proper error handling
if ! id -u $user >/dev/null 2>&1; then
echo "Creating user $user with UID $uid"
adduser -u $uid -m -s /bin/bash $user || {
echo "Failed to create user $user with UID $uid"
exit 1
}
else
echo "User $user already exists"
fi
mkdir -p /home/$user/app mkdir -p /home/$user/app
mkdir -p /home/$user/logs/{nginx,nodejs} mkdir -p /home/$user/logs/{nginx,nodejs}
@@ -64,15 +73,25 @@ fi
# Now there's always an app in the user directory (either user's or example) # Now there's always an app in the user directory (either user's or example)
cd /home/$user/app cd /home/$user/app
# Verify user exists and show info
echo "Verifying user setup:"
id $user || { echo "ERROR: User $user does not exist!"; exit 1; }
# Install dependencies as the user
echo "Installing npm dependencies as user $user..."
su -c "npm install" $user su -c "npm install" $user
# Check if ecosystem.config.js exists, if not generate it # Check if ecosystem.config.js exists, if not generate it
if [ ! -f /home/$user/app/ecosystem.config.js ]; then if [ ! -f /home/$user/app/ecosystem.config.js ]; then
echo "No ecosystem.config.js found, generating from package.json..." echo "No ecosystem.config.js found, generating from package.json..."
/scripts/generate-ecosystem-config.sh "$user" "/home/$user/app" /scripts/generate-ecosystem-config.sh "$user" "/home/$user/app"
chown $user:$user /home/$user/app/ecosystem.config.js
fi fi
su -c "pm2 start ecosystem.config.js" $user # Start PM2 as the user with HOME environment set
echo "Starting PM2 as user $user..."
su -c "cd /home/$user/app && HOME=/home/$user pm2 start ecosystem.config.js --no-daemon" $user &
# Follow logs # Follow logs
tail -f /home/$user/logs/nginx/* /home/$user/logs/nodejs/* tail -f /home/$user/logs/nginx/* /home/$user/logs/nodejs/*