Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pm2 does not restart app when Centos server reboot #1521

Closed
thanhthang20 opened this issue Aug 12, 2015 · 15 comments
Closed

pm2 does not restart app when Centos server reboot #1521

thanhthang20 opened this issue Aug 12, 2015 · 15 comments

Comments

@thanhthang20
Copy link

screen shot 2015-08-12 at 10 52 53 pm

@jwhitmarsh
Copy link

👍 same here

@jshkurti
Copy link
Contributor

@soyuka @Tjatse

@IvanGaravito
Copy link

Using Ubuntu 14.04.1 got similar behaviour for # sudo env PATH=$PATH:/usr/bin pm2 startup -u safeuser. Inspecting boot log I've found the following:

Starting pm2
events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: connect EACCES
    at exports._errnoException (util.js:746:11)
    at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)

Using # sudo env PATH=$PATH:/usr/bin pm2 startup and adding the app with # sudo pm2 start app.json works perfectly after reboot, but it's not secure run pm2 as root.

A note of worth is that reading https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps it points to use libcap2-bin library to allow use restricted ports, but remains the same behaviour as not executing # sudo setcap cap_net_bind_service=+ep /usr/bin/node.

@ungana
Copy link

ungana commented Aug 31, 2015

I'm running into the same thing.

I did come across this article, but it didn't really seem to help much. I did however get it to startup once on reboot but not since.

  • CentOS Linux release 7.1.1503 (Core)

I'm not sure if it makes a difference, but just so you have more info; I am starting this from a config (JSON) file.

{
  "apps": [
    {
      "name": "express-app-production",
      "script": "bin/www",
      "env": {
        "NODE_ENV": "production",
        "PORT": "3000"
      }
    }
  ]
}

@dibari
Copy link

dibari commented Sep 18, 2015

I ran into this issue, and found a solution that works for me. Let's assume I've created a user named 'node' to run pm2. All of the commands below should be run by the node user, in the node user's home directory (/home/node)

  1. Clear any pm2 dump files, and then start the appropriate services
    % pm2 delete all
    % pm2 dump
    % pm2 start app.js --watch
  2. Save the current config to start on reboot
    % pm2 save
    % sudo pm2 startup centos -u node
  3. Edit the PM2_HOME path in the pm2-init script to match the home directory of the user:
    % sudo vi /etc/init.d/pm2-init.sh
    export PM2_HOME="/home/node/.pm2"

I found the issue was, the user was being set appropriately in the pm2-init.sh file, but the PM2_HOME directory was set to /root/.pm2 by default

==UPDATE==

For centos, I was able to replace step 2 above with the following:
% sudo su -c "env PATH=$PATH:/usr/local/bin PM2_HOME=/home/node/.pm2 pm2 startup centos -u node"

@mikalai-s
Copy link

@dibari thank you! it worked out for me too!

Update: while it worked at the first time, it stopped working right after. The issue is that dump.pm2 is being emptied on reboot so nothing is resurrected on startup. So as a workaround I followed this suggestion to make the file immutable.

@yizhiheng
Copy link

@dibari
I have been stuck here for 3 days....Thank you very much for your solution!!! It also worked for me!!!

@crystoll
Copy link

Same problem here. To me it seems like dumpfile gets emptied on reboot. If I run resurrect manually as proper user, it all works. I'm using pm2-generated init file, with non-root user who has all node tools and pm2 installed under his account.
This is somewhat suprising issue to have.

@ghartsho
Copy link

@mikalai-silivonik thanks should go to you here, on CentOS7

chattr +i [USER_HOME_DIR]/.pm2/dump.pm2 did the trick for me after the pm2 save

@julianxhokaxhiu
Copy link

@ghartsho your chattr command saved my life, thanks!

@meteormatt
Copy link

@thanhthang20 I try success by this step

pm2 start start.js
pm2 save
pm2 startup centos

@y4my4my4m
Copy link

@meteormatt
I get this error trying to do pm2 startup centos

[PM2] Writing startup script in /etc/init.d/pm2-init.sh
[PM2] Making script booting at startup...
[PM2] /var/lock/subsys/pm2-init.sh lockfile has been added
[PM2] -centos- Using the command:
      su -c "chmod +x /etc/init.d/pm2-init.sh; chkconfig --add pm2-init.sh"
internal/child_process.js:313
    throw errnoException(err, 'spawn');
    ^

Error: spawn ENOMEM
    at exports._errnoException (util.js:1036:11)
    at ChildProcess.spawn (internal/child_process.js:313:11)
    at exports.spawn (child_process.js:380:9)
    at Object.exports.execFile (child_process.js:143:15)
    at exports.exec (child_process.js:103:18)
    at CLI.startup (/usr/local/lib/node_modules/pm2/lib/API/Startup.js:175:5)
    at Command.<anonymous> (/usr/local/lib/node_modules/pm2/bin/pm2:548:9)
    at Command.listener (/usr/local/lib/node_modules/pm2/node_modules/commander/index.js:301:8)
    at emitTwo (events.js:106:13)
    at Command.emit (events.js:191:7)

Any ideas?

@meteormatt
Copy link

@ubiko Maybe no enough memory?

@y4my4my4m
Copy link

@meteormatt that must be it!

I'm on a VPS with no control over the swap.
Thanks, I'll change my setup!

@Unitech
Copy link
Owner

Unitech commented Dec 6, 2016

Closing in favor of #2559

To try the new startup system:

$ npm install Unitech/pm2#development -g
$ pm2 update
$ pm2 startup     # Then copy / paste the command displayed
$ pm2 unstartup

@Unitech Unitech closed this as completed Dec 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests