How do you use pm2 startup with a non-root user?

10,239

Solution 1

(Posted on behalf of the OP).

In fact that was the problem. Fixed via creating a symlink (as root):

ln -s /usr/bin/nodejs /usr/sbin/node

Solution 2

I don't know if it will help you but I use in this way:


  1. As a non-root user
pm2 startup -u <YOUR_NON_ROOT_USER>
  1. Copy line showed like
env PATH=$PATH:/usr/bin pm2 startup systemd -u delivery --hp /home/delivery
  1. As a root execute
env PATH=$PATH:/usr/bin pm2 startup systemd -u delivery --hp /home/delivery
  1. Back to non root user and type:
pm2 start <YOUR /PATH/TO/INDEX.JS> --name <YOU_APPLICATION_NAME>
  1. As a non-root type:
pm2 save
  1. reboot
sudo reboot
  1. As a non-root user type the commando bellow to check if it works
pm2 status

PS: Change as needed.

I hope it will be useful for you or someone.

Share:
10,239
Zach Smith
Author by

Zach Smith

I am a Software Developer with experience across a wide range of technologies. I graduated with an MSc in Information Technology from the University of Cape Town, where I worked on alternative methods of scaling traditional (relational) systems using NoSQL data stores. Currently I fulfill the role of a full stack developer, making the most delightful things.

Updated on June 16, 2022

Comments

  • Zach Smith
    Zach Smith almost 2 years

    According to the documentation here: http://pm2.keymetrics.io/docs/usage/startup/#startup-systems-support

    You can use the command pm2 startup ubuntu -u nodeapps to resurrect all saved pm2 jobs on server startup.

    I ran this command as the nodeapps user. Then I was given a sudo su command to run. I logged out of nodeapps, used sudo su to log into the system as root, and ran the command:

    sudo su -c "env PATH=$PATH:/usr/bin pm2 startup ubuntu -u nodapps --hp /home/nodeapps"
    

    The processes did not restart on server restart. I found this question on Stack Overflow: Ubuntu 14.04 - pm2 startup not starting after reboot.

    In the script /etc/init.d/pm2-init.sh I found the line that question recommended addressing:

    export PATH=/usr/bin:$PATH
    export PM2_HOME="/home/nodeapps/.pm2"
    

    But it looks correct to me so I didn't change anything.

    I then found this question: pm2 Startup not starting up on Ubuntu

    and in my boot logs I find the following line:

    Starting pm2
    /usr/bin/env: node: No such file or directory
    

    I know that 'node' on Ubuntu is actually 'nodejs'. Could this be the reason?

    If it is, what can I do to make the startup command look for nodejs instead of node.

    Alternatively, could this be a $PATH problem? If it is, how can I add the correct path to root (at least I think it should be added to root)

  • Hasan Alsawadi
    Hasan Alsawadi about 3 years
    Humm... tipe ? Do you mean type ?! Very good hints. Thank You!
  • Admin
    Admin about 3 years
    Hi Hasan. I just corrected it. The correct one was "type". Thanks for your comment and sorry for the mistake.
  • dwaz
    dwaz about 3 years
    @RodrigoGaldino. Thank you. This answer works!