pm2 does not restart app when Centos server reboot

Created on 12 Aug 2015  路  15Comments  路  Source: Unitech/pm2

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

Icebox Startup

Most helpful comment

@thanhthang20 I try success by this step

pm2 start start.js
pm2 save
pm2 startup centos

All 15 comments

:+1: same here

@soyuka @Tjatse

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.

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"
      }
    }
  ]
}

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"

@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.

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

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.

@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

@ghartsho your chattr command saved my life, thanks!

@thanhthang20 I try success by this step

pm2 start start.js
pm2 save
pm2 startup centos

@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?

@ubiko Maybe no enough memory?

@meteormatt that must be it!

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

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
Was this page helpful?
0 / 5 - 0 ratings

Related issues

xzycn picture xzycn  路  3Comments

morfies picture morfies  路  3Comments

webchaz picture webchaz  路  3Comments

rajendar38 picture rajendar38  路  3Comments

liujb picture liujb  路  3Comments