Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 324
Sascha Avatar asked Sep 25 '15 11:09

Sascha


People also ask

Where is the autostart file in Raspbian?

After your desktop environment starts (LXDE-pi, in this case), it runs whatever commands it finds in the profile's autostart script, which is located at /home/pi/. config/lxsession/LXDE-pi/autostart for our Raspberry Pi. Note that the directory pi might be different if you created a new user for your Raspberry Pi.


1 Answers

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

like image 166
Sascha Avatar answered Sep 18 '22 08:09

Sascha