Start app as root with pm2

Martin Nilsson picture Martin Nilsson · Jan 30, 2016 · Viewed 34.1k times · Source

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?

Answer

Elias Fyksen picture Elias Fyksen · Feb 9, 2018

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