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 !
Most helpful comment
:+1: for running as non-root user.