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?
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?
You should start pm2 as a root, (sudo pm2 start app.js), then your app will start as a root
First, install pm2 globally. Then set root permissions to pm2 using this command
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 useryourusername
to have access to port 80, run:And then use
authbind --deep pm2
instead ofpm2
. The documentation suggests setting up an alias.I would recommend:
OR
OR
To start it on your HTTP Port
Also, I always put
-i 0
at the end - this starts up as many worker processes as you have cores. Check THISIt is not always necessary to start PM2 as root. If you have PM2 as root and the cli module installed, security is a big risk. This is only required if you're starting your app on a port between 1 and 1024
I had problems with
sudo pm2 start api
, but this was since pm2 was already running without sudo privileges, therefor you need to run: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