7

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

Sascha
  • 581
  • 1
  • 4
  • 19

6 Answers6

18

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

Sascha
  • 581
  • 1
  • 4
  • 19
10

I was able to get PM2 to start at bootup correctly on my RPi with this command:

sudo env PATH=$PATH:/usr/local/bin pm2 startup systemd -u pi --hp /home/pi

The --hp /home/pi part seemed to be the difference. That is what was left out of a lot of solutions I found that didn't work.

Chris Troutner
  • 477
  • 6
  • 7
  • 1
    After trying all the other answers, this one worked flawlessly. After a reboot, pm2 now starts cncjs (in my case). – Fusseldieb Feb 11 '20 at 19:50
1

I also searched for days without success but then, I got it to work quite simply.


  1. In Raspian click Menu,Preferences,Main Menu Editor
  2. Click Preferences and check Default applications for LXSession
  3. Click OK and close main menu editor
  4. Now click Menu and Under Preferences click on Default applications for LXSession
  5. LXSession configuration opens
  6. Click Autostart
  7. Under Manual autostarted applications paste in your java command line
  8. click add
  9. close the LXSession configuration application and reboot your pi

your java app should run after reboot

Palmeta
  • 61
  • 5
0

Here's a workaround based on this article (the suggested workaround didn't help me):

  1. Make a mini startup init.d script to resurrect the pm2 process yourself

    #! /bin/sh
    # /etc/init.d/pm2
    #
    # help documentation: 
    #https://debian-administration.org/article/28/Making_scripts_run_at_boot_time_with_Debian
    
    pm2 resurrect
    
  2. Make it executable

    chmod 755 /etc/init.d/blah
    
  3. Update system symbolic links

    update-rc.d pm2 defaults
    
  4. Reboot, go to your website (and look at the date; make sure it's not browser cached)

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Bert
  • 319
  • 4
  • 10
0

I'm going to describe the debug steps that led me to the solution, since I think that it can be usefull. If you just want the solution I invite you to go to the end of this answer.

Debug steps

In my case I was setting up pm2 startup like this:

sudo pm2 startup systemd -u pi

And it wasn´t working. pm2 just refused to start at boot. In order to know what was going on I followed this instructions.

Next I had a look at the systemctl units:

 systemctl list-units

And saw this red line in the output

pm2-pi.service    failed failed    PM2 process manager

Then I had a look to the corresponding log (note the username at the end of the filename: pm2-pi)

journalctl -u pm2-pi

So the reason was that pi user had not enugh permissions to access the JSON module conf file.

May 28 12:36:52 raspberrypi pm2[534]: Error: EACCES: permission denied, open '/root/.pm2/module_conf.json'

Solution

I could have changed the json permissions but I decided to run PM2 as root user. If this is dangerous, please let me know.

sudo pm2 startup -u root
Marcos R
  • 733
  • 7
  • 8
0

My issue was to do with user names, using root rather than my non-root user. I fixed it by editing the service file directly. There's probably a better way to fix it e.g. by running the startup command differently. But a simple work-around (manual file change) worked.

When I ran the startup command:

sudo pm2 startup systemd -u XXX

It created a file for the reboot service here:

/etc/systemd/system/pm2-XXX.service

In this file some of the paths are for root and not my user XXX:

Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid

So I changed the paths to that of the correct user:

Environment=PM2_HOME=/home/XXX/.pm2
PIDFile=/home/XXX/.pm2/pm2.pid

And after a reboot the process starts up as expected.

Moika Turns
  • 677
  • 11
  • 17