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
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
I been searching for work around for quit while till i came to this solution
after executing pm2 save
this is working fine for me now
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.
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.
What steps work for me on Ubuntu 18.04.4
npm i pm2 -g
pm2 startup
pm2-root
found in /etc/systemd/system
systemctl enable pm2-root
, service pm2-root status
pm2 unstartup
, pm2 startup
; documentationpm2 start dist\index.js --name YourNodeApp
pm2 save
reboot
pm2 ls
should show your YourNodeApp
online