Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start app as root with pm2

Tags:

node.js

root

pm2

I have a daemon that must be run as root on startup.

I use pm2 to start other apps but can not figure out if it can start an app as root. Can it be done?

If not, what are my options?

like image 585
Martin Nilsson Avatar asked Jan 30 '16 17:01

Martin Nilsson


People also ask

Does pm2 start on boot?

PM2 is designed to work with the default init system on a Linux system (which it can auto-detect) to generate the startup script and configure PM2 as a service that can be restarted at system boot. The startup sub-command tells PM2 to detect available init system, generate configuration and enable the startup system.


2 Answers

I had problems with sudo pm2 start api, but this was since pm2 was already running without sudo privileges, therefor you need to run:

pm2 kill sudo pm2 start api 

This kills the pm2 deamon first, so that it starts in sudo, but then you need sudo for ALL pm2 commands afterwards, like: sudo pm2 ls

like image 101
Elias Fyksen Avatar answered Sep 18 '22 17:09

Elias Fyksen


If you only need your daemon to be run as root in order to access a port number (such as 80 or 443), the pm2 documentation recommends using authbind. So, if you want the user yourusername to have access to port 80, run:

$ sudo apt-get install authbind $ sudo touch /etc/authbind/byport/80 $ sudo chown yourusername /etc/authbind/byport/80 $ sudo chmod 755 /etc/authbind/byport/80 $ authbind --deep pm2 update 

And then use authbind --deep pm2 instead of pm2. The documentation suggests setting up an alias.

like image 42
Rabbits Avatar answered Sep 17 '22 17:09

Rabbits