Ubuntu 14.04 - pm2 startup not starting after rebo

2019-02-10 01:05发布

问题:

I am using pm2 to start a node.js process, and I would like this process to be started automatically when the system (Ubuntu 14.04 on Intel Atom processor) is booted. I have followed the instructions on the pm2 web site but without success. My dump.pm2 appears to be correct, but the pm2 daemon is never started. I have tried both the ubuntu and linux options to pm2 startup, as well as autodetect, both with and without a specified user. Files (links) S20pm2-init.sh have been created in /etc/rc[2-5].d but nothing gets started. I can't find find any obvious error messages in the system log.

Any ideas?

Edit to add

I find that service pm2-init.sh start starts the daemon correctly, when run from my login account, which is the same as that defined by USER= in the file /etc/init.d/pm2-init.sh. I've made sure the PM2_HOME definition in this file points to the correct place as well. However, it still doesn't start on system boot!

回答1:

For pm2 startup to work, there's a critical command buried in documentation and here.

You have to 1) start all your processes, 2) create the startup script, and 3) run pm2 save

Reference to @lazlojuly's answer here.



回答2:

Well, I think this worked, anyway. I was originally trying to start as a system user (no login shell) called node-red running the node application of the same name, and when that didn't work I tried starting as ordinary user max. After some fiddling with things it suddenly started working, but on this slow Atom processor, the node application starts only a couple of minutes after the desktop appears.

To get it running under the system user: first, logged in as max, I deleted all pm2 jobs and ran pm2 save to create a empty dump file. Then I did:

sudo npm install pm2@latest -g
pm2 update

To get the latest version, in case that helped. I moved from 0.14.7 to 0.15.7

Then I ran

sudo su -c "env PATH=$PATH:/usr/bin pm2 startup ubuntu -u node-red"

and edited /etc/init.d/pm2-init.sh correcting the line that sets the PM2_HOME directory to the home directory of node-red:

export PM2_HOME="/home/node-red/.pm2"

Then I became a login version of the node-red user, changed to that user's home directory, started my job, and saved the process list:

sudo -H -u node-red bash -l
cd
pm2 start /usr/local/bin/node-red --node-args="--max-old-space-size=128" -- -v -u /home/node-red/.node-red
pm2 save

Then I exited from node-red's shell, rebooted, and (after a minute or so's delay) there was the application running nicely!

Note that this was ubuntu 14.04, which uses the init/upstart system. Later versions I think use systemd approach and may need the ubuntu parameter changed to systemd when creating the startup script.

I'm still not really sure why it didn't work properly the first time, though.



回答3:

I been searching for work around for quit while till i came to this solution

after executing pm2 save

  1. this file will be created -> /etc/init.d/pm2-init.sh
  2. then on cron tab @reboot root /etc/init.d/pm2-init.sh start this equivalent to executing it on cmd but triggered on reboot

this is working fine for me now