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?
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With