Pm2: Provide a way to run pm2 without root

Created on 25 Sep 2013  路  7Comments  路  Source: Unitech/pm2

Feature Request: Init scripts are nice, but it would be much better if we had a way to run pm2 and start it on reboot automatically. This could be done using cron and checking every couple of seconds if pm2 is actually up.

Perl community has a similar solution in ubic

Most helpful comment

:+1: for running as non-root user.

All 7 comments

It would be an interesting solution instead of init script. I will think about it, thanks for the concept

:+1: for running as non-root user.

I have been digging in to this.

On Linux distros that use upstart instead of sysvinit (Ubuntu, CentOS 6), or can install upstart (Debian). You can use the setuid upstart configuration stanza in an upstart config file to run as a non-root user.

I'm working on getting this running. It definitely works best with n instead of nvm as a node binary manager.

I'll update as I learn more.

@Unitech Would you be open to passing in a user name to the init script? I was able to easily accomplish this.

Maybe the pm2 command could be something like pm2 startup platform user?

Here is what I hacked up (I think my super() function could use some refinement with respect to passing in three arguments, but I was being lazy):

#!/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

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

export HOME="/home/$USER"

super() {
    su -l $USER -c "$1 $2 $3"
}

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

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

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

status() {
    echo "Status for $NAME:"
    super $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

Great ! I will integrate this ! Thanks (:

If you would like, I'd be happy to cook up a PR.

I integrated it and added a whoami to get the current user, thanks for your help !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ldarren picture ldarren  路  3Comments

morfies picture morfies  路  3Comments

phra picture phra  路  3Comments

jubairsaidi picture jubairsaidi  路  3Comments

webchaz picture webchaz  路  3Comments