PM2 - Autostart on Raspbian (Raspberry Pi) does not work

Sascha picture Sascha · Sep 25, 2015 · Viewed 19.1k times · Source

Ok guys,

this is driving me nuts... Can't get my nodejs application to autostart@boot on a raspberry pi...

Machine: Raspberry 2 (Raspbian Jessie)

Tried almost every possible solution I found on Google.

This is what I've ended up with:

Installed pm2

$ sudo npm install -g pm2

This will install it as a init.d script and run the application as pi user

$ sudo pm2 startup raspberry -u pi
[PM2] Generating system init script in /etc/init.d/pm2-init.sh
[PM2] Making script booting at startup...
[PM2] -raspberry- Using the command:
  su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults"
[PM2] Done.

I've read that the script refers to the wrong .pm2 folder (looks in the root folder, not the user's folder) so I changed that

 $ sudo vi /etc/init.d/pm2-init.sh

Changed export PM2_HOME="/root/.pm2" to export PM2_HOME="/home/pi/.pm2"

$ cd /opt/mycoolnodeproject

Starting my node project with pm2

$ pm2 start server.js -x --name "node-project"

Save active processes so it will restart them upon restart (if the pi crashes, it would save it on shutdown automatically)

$ pm2 dump

So now the server is up and running and works fine... until I reboot! I thought that pm2 would autostart my node application but for some reason it doesn't... :(

Any idea what the problem might be?

Btw: I've also tried to call startup like this sudo env PATH=$PATH:/usr/local/bin pm2 startup raspberry -u pi, but that didn't work either.

Regards,

Sascha

Answer

Sascha picture Sascha · Oct 6, 2015

Mia's comment made me checking the issue list of PM2 again and someone figured out how to do it! :)

https://github.com/Unitech/pm2/issues/1654

The solution:

sudo pm2 startup systemd -u <username>

Works like a charm! :)

Sascha