Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

edalzell's avatar

Run PM2 as a daemon

I'm trying to use PM2 and make sure it's always running and restarts w/ code changes.

It looks like I need to run pm2 start /home/forge/staging.fitnessmd.fit/staging-socket.js --watch to do that, however when I set this up as a daemon I get the following error:

[PM2][Initialization] Environment variable HOME (Linux) or HOMEPATH (Windows) are not set!
[PM2][Initialization] Defaulting to /etc/.pm2
Error: EACCES: permission denied, mkdir '/etc/.pm2'

What am I doing wrong, or what is the correct way to run pm2 on Forge?

Thanks

0 likes
11 replies
edalzell's avatar

Same error unfortunately, going to try the pm2 startup command to see what it generates

Sinnbeck's avatar

Happy to help :) mark your own answer as best

edalzell's avatar

That doesn't work, deleted it. It shows it starts up, but running pm2 list on the command does not show it running. I'm guessing it's not running it as forge, even though that's what I said when creating the daemon.

Sinnbeck's avatar

Never used forge, but can you run things as sudo?

sudo -u forge pm2 start /home/forge/staging.fitnessmd.fit/staging-socket.js - - watch
Sinnbeck's avatar

Out of curiosity. Do you know what user is running the commands? Is it the forge user?

edalzell's avatar

It should be forge. For the life of me I cannot figure out how to ensure this restarts at server reboot. I have a ticket into Forge we'll see what they say.

Please or to participate in this conversation.