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

6

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

Acrophobia answered 25/9, 2015 at 11:27 Comment(10)
did you install pm2 with sudo?Galvanometer
sudo npm install -g pm2Galvanometer
I'm having similar issues -- would love to know if you somehow managed to solve your problem.Galvanometer
Sure, I installed it with sudo. I can't install global deps without sudo.Acrophobia
did you find a solution so far?Galvanometer
Nope. Nothing... :( Also added my problem to an existing issue @ github. No response so far. :(Acrophobia
I had no response so far as well. I hate spending time for these small details that "has to" work normally -- but still... trying to figure out. It was working fine before jessie.Galvanometer
Hey Mia, ok that's interesting. So it's probably related to Jessie? I upgrade right after the installation, so I had no idea. Good to know!Acrophobia
I added the solution to this thread! :)Acrophobia
haha, it's a solutiın I fund and posted there :]Galvanometer
A
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

Acrophobia answered 6/10, 2015 at 18:13 Comment(1)
It's me :) I am glad it helped.Galvanometer
W
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.

Word answered 18/12, 2016 at 2:7 Comment(1)
After trying all the other answers, this one worked flawlessly. After a reboot, pm2 now starts cncjs (in my case).Bridgework
C
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

Castillo answered 29/2, 2016 at 15:20 Comment(1)
Java? Am I missing something?Talanian
J
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)

Jacquenette answered 25/3, 2017 at 23:50 Comment(0)
N
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
Neurosis answered 28/5, 2020 at 11:17 Comment(0)
K
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.

Kevyn answered 13/8, 2022 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.