How do I host node.js apps with pm2 without running them as root?

Posted by jishi on Server Fault See other posts from Server Fault or by jishi
Published on 2013-10-01T20:31:02Z Indexed on 2013/10/23 3:57 UTC
Read the original article Hit count: 866

Filed under:
|
|
|

I have setup pm2 to run a node.js application, and I can successfully start it and it will resurrect upon reboot.

However, the pm2 daemon is ran as root, which makes me think that all my node-scripts also runs as root? Even though I added them as a regular user in the system. The log files and stuff is created in the users home dir, /~/.pm2/logs, but the logs are owned by root.

when I invoke pm2 startup (which handles the installation of the init.d script etc), it creates /etc/init.d/pm2-init.sh which looks like this:

#!/bin/bash
# chkconfig: 2345 98 02
#
# description: PM2 next gen process manager for Node.js
# processname: pm2
#
### BEGIN INIT INFO
# Provides:          pm2
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description: PM2 init script
# Description: PM2 is the next gen process manager for Node.js
### END INIT INFO

NAME=pm2
PM2=/usr/local/lib/node_modules/pm2/bin/pm2
NODE=/usr/local/bin/node

export HOME="/root"

start() {
    echo "Starting $NAME"
    $NODE $PM2 stopAll
    $NODE $PM2 resurrect
}

stop() {
    $NODE $PM2 dump
    $NODE $PM2 stopAll
}

restart() {
    echo "Restarting $NAME"
    stop
    start
}

status() {
    echo "Status for $NAME:"
    $NODE $PM2 list
    RETVAL=$?
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        restart
        ;;
    *)
        echo "Usage: {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $RETVAL

When I dump the processes (which is what it will use when resurrecting the processes), I see mentions of user "USER":"pi" but I don't think that it's actually run as user pi.

Any thoughts?

© Server Fault or respective owner

Related posts about linux

Related posts about node.js