pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

running pm2 startup without $HOME variable ends in 100% CPU loop

Open JNK opened this issue 10 years ago • 7 comments

When running the pm2 startup command without the $HOME environment variable the command fails by ending in an endless loop of 100% CPU usage. I came across this error by accident when I was trying to install pm2 with puppet:

exec { 'create pm2 startup script':
    command => '/usr/local/bin/pm2 startup ubuntu',
    # environment => ["HOME=/root"], # uncomment for it to work
    user => 'root',
    group => 'root',
    creates => '/etc/init.d/pm2-init.sh',
  }

As soon as I added environment => ["HOME=/root"] it worked like designed. I would suggest implementing a check to see wether the variable is set and exiting with error if it is missing. It was quite frustrating to debug this and I was about to give up on pm2...

JNK avatar Dec 07 '15 12:12 JNK

PM2 needs to know where is your HOME path, else it cannot interact with the current daemon.

There are already several checks and a fallback to /etc folder,

So you would suggest than instead of doing a fallback on /etc, PM2 should throw an error there?

Unitech avatar Dec 08 '15 17:12 Unitech

Yes, as it turned out to not work (loop) when trying to call pm2 startup from exec in puppet. I can create an example puppet script to show you what happens if you'd like

JNK avatar Dec 11 '15 17:12 JNK

@Unitech , agreed that a home should be set, but if it's not, the default to /etc/ seems to be broken.

Note that the crux of this argument is whether HOME is set to the empty string (resulting in '') or unset (resulting in undefined, which when cast to a string is 'undefined'):

root@test-start-2:/root/.pm2/logs# export HOME=                                                                                                                                 
root@test-start-2:/root/.pm2/logs# node -e "console.log(process.env.HOME);"                                                                                                     

root@test-start-2:/root/.pm2/logs# pm2 start
[PM2][Initialization] Environment variable HOME (Linux) or HOMEPATH (Windows) are not set!
[PM2][Initialization] Defaulting to /etc/.pm2
[PM2] Spawning PM2 daemon
[PM2] PM2 Successfully daemonized

  error: missing required argument `file|json|stdin|app_name|pm_id'

And with the environment variable unset:

root@test-start-2:/root/.pm2/logs# unset HOME
root@test-start-2:/root/.pm2/logs# node -e "console.log(process.env.HOME);"
undefined
root@test-start-2:/root/.pm2/logs# pm2 start
[PM2][Initialization] Environment variable HOME (Linux) or HOMEPATH (Windows) are not set!
[PM2][Initialization] Defaulting to /etc/.pm2
[PM2] Spawning PM2 daemon
^C  # <-- NOTE: Here is where I had to kill the process

Looking at the logs:

root@test-start-2:/root/.pm2/logs# cat /etc/.pm2/pm2.log 
[PM2][Initialization] Environment variable HOME (Linux) or HOMEPATH (Windows) are not set!
[PM2][Initialization] Defaulting to /etc/.pm2
2016-01-19 15:53:34: [PM2][WORKER] Started with refreshing interval: 30000
2016-01-19 15:53:34: [[[[ PM2/God daemon launched ]]]]
2016-01-19 15:53:34: BUS system [READY] on port /etc/.pm2/pub.sock
2016-01-19 15:53:34: RPC interface [READY] on port /etc/.pm2/rpc.sock
2016-01-19 15:53:58: PM2 is being killed via kill method
2016-01-19 15:53:58: RPC socket closed
2016-01-19 15:53:58: PUB socket closed
2016-01-19 15:54:23: [PM2][WORKER] Started with refreshing interval: 30000
2016-01-19 15:54:23: Error: ENOENT: no such file or directory, open '/root/.pm2/logs/undefined/.pm2/pm2.pid'
    at Error (native)
    at Object.fs.openSync (fs.js:549:18)
    at Object.fs.writeFileSync (fs.js:1156:15)
    at Object.Satan.processStateHandler (/usr/lib/node_modules/pm2/lib/Satan.js:98:8)
    at Object.Satan.remoteWrapper (/usr/lib/node_modules/pm2/lib/Satan.js:130:9)
    at Domain.<anonymous> (/usr/lib/node_modules/pm2/lib/Satan.js:682:13)
    at Domain.run (domain.js:228:14)
    at Object.<anonymous> (/usr/lib/node_modules/pm2/lib/Satan.js:681:7)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
2016-01-19 15:54:23: [[[[ PM2/God daemon launched ]]]]

Any chance we can update this to treat an undefined process.env.HOME as '' ?

jgeewax avatar Jan 19 '16 16:01 jgeewax

#1888

soyuka avatar Jan 19 '16 16:01 soyuka

@JNK I release PR for #1888 today :wink:

estliberitas avatar Jan 19 '16 17:01 estliberitas

@estliberitas awesome :+1: :smiley:

JNK avatar Jan 19 '16 17:01 JNK

fixed via https://github.com/Unitech/pm2/pull/1911

Unitech avatar Nov 18 '16 11:11 Unitech