Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 499
Zach Smith Avatar asked Jun 02 '16 09:06

Zach Smith


2 Answers

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.

like image 65
Rodrigo Luiz Galdino Camargo Avatar answered Sep 18 '22 12:09

Rodrigo Luiz Galdino Camargo


(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
like image 32
halfer Avatar answered Sep 19 '22 12:09

halfer