Ubuntu 14.04 - pm2 startup not starting after reboot
Asked Answered
K

5

10

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!

Katharinakatharine answered 24/9, 2015 at 19:22 Comment(1)
I'm using pm2 on Ubuntu 15.04, I tried all the possible solutions start the save but nothing when rebooting my server, please advice?Forgot
P
8

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.

Preemie answered 10/6, 2016 at 0:47 Comment(2)
This works great for me - without any custom hack or customization to startup script in my Digital Ocean Ubuntu droplet.Preemie
+1 Crazy this is not included in their startup documentationSwipple
S
7

What steps work for me on Ubuntu 18.04.4

  1. npm i pm2 -g
  2. run this command as root user: pm2 startup
    • creates service pm2-root found in /etc/systemd/system
  3. systemctl enable pm2-root, service pm2-root status
    • Note: after Node upgrade, run these commands as root: pm2 unstartup, pm2 startup; documentation
  4. pm2 start dist\index.js --name YourNodeApp
  5. pm2 save
  6. reboot
  7. pm2 ls should show your YourNodeApp online
Steppe answered 7/2, 2020 at 19:5 Comment(0)
K
3

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.

Katharinakatharine answered 1/10, 2015 at 21:4 Comment(1)
this line here export PM2_HOME="/home/node-red/.pm2" was the fix for me. it was export PM2_HOME="/root/.pm2" for some reasonKoreykorff
D
1

Basically,

Step 1: Run following command,

pm2 startup

Here you will get 1 command generated, copy/paste it and done.

(Sample command you will get from above step: sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu) Ref

Dordogne answered 20/11, 2020 at 20:15 Comment(0)
O
0

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

Ourself answered 15/12, 2016 at 22:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.