running pm2 startup without $HOME variable ends in 100% CPU loop
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...
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?
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
@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 '' ?
#1888
@JNK I release PR for #1888 today :wink:
@estliberitas awesome :+1: :smiley:
fixed via https://github.com/Unitech/pm2/pull/1911