Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pm2 startup not working after updating Node #2006

Closed
pensierinmusica opened this issue Mar 9, 2016 · 7 comments
Closed

pm2 startup not working after updating Node #2006

pensierinmusica opened this issue Mar 9, 2016 · 7 comments

Comments

@pensierinmusica
Copy link

Hi,

I've pm2 set up with a few processes, that have been saved, then ran pm2 startup ubuntu.
It was all working fine: on every server reboot pm2 would restart the processes.

Recently though I updated Node and pm2.

Then, I noticed that the pm2 was not starting up anymore on reboot. I'm not sure if this is due to updating the Node version, or pm2 itself.

In either case I think this is highly undesirable, and it should be fixed. If I set up pm2 to automatically start on reboot, I expect this behaviour to hold true after updating Node or pm2.

Could you please check? Thanks!

p.s. Imho a good suspect for this bug is that the Node executable path changes after a Node update

@Unitech
Copy link
Owner

Unitech commented Mar 9, 2016

Hello,

Yes it may come from an update of Node.js.
Do you use NVM?
Can you check the file /etc/init.d/pm2-init.sh?

@pensierinmusica
Copy link
Author

Hi @Unitech, yes I'm using nvm.

This is the content of pm2-init.sh:

#!/bin/bash
# chkconfig: 2345 98 02
#
# description: PM2 next gen process manager for Node.js
# processname: pm2
#
### BEGIN INIT INFO
# Provides:          pm2
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# 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/nvm/versions/node/v5.7.1/lib/node_modules/pm2/bin/pm2
USER=admin
DEFAULT=/etc/default/$NAME

export PATH=/usr/local/nvm/versions/node/v5.7.1/bin:$PATH
export PM2_HOME="/home/admin/.pm2"

# The following variables can be overwritten in $DEFAULT

# maximum number of open files
MAX_OPEN_FILES=

# overwrite settings from default file
if [ -f "$DEFAULT" ]; then
      . "$DEFAULT"
fi

# set maximum open files if set
if [ -n "$MAX_OPEN_FILES" ]; then
    ulimit -n $MAX_OPEN_FILES
fi

get_user_shell() {
    local shell=$(getent passwd ${1:-`whoami`} | cut -d: -f7 | sed -e 's/[[:space:]]*$//')

    if [[ $shell == *"/sbin/nologin" ]] || [[ $shell == "/bin/false" ]] || [[ -z "$shell" ]];
    then
      shell="/bin/bash"
    fi

    echo "$shell"
}

super() {
    local shell=$(get_user_shell $USER)
    su - $USER -s $shell -c "PATH=$PATH; PM2_HOME=$PM2_HOME $*"
}

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

stop() {
    #super $PM2 dump
    super $PM2 delete all
    super $PM2 kill
}

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

reload() {
    echo "Reloading $NAME"
    super $PM2 reload all
}

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

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

Any idea how the issue could be fixed? Thanks! Cheers

@Unitech
Copy link
Owner

Unitech commented Mar 14, 2016

Please review it by yourself depending on your Node.js version, PM2 paths and so on, thank you

@pensierinmusica
Copy link
Author

Yes, the Node executable path and the pm2 package path change each time Node is updated through nvm.

Here's an example of how paths respectively change:

# Node 5.7
/usr/local/nvm/versions/node/v5.7.1/bin/node
/usr/local/nvm/versions/node/v5.7.1/lib/node_modules/pm2/bin/pm2

# Node 5.9
/usr/local/nvm/versions/node/v5.9.0/bin/node
/usr/local/nvm/versions/node/v5.9.0/lib/node_modules/pm2/bin/pm2

Currently this breaks the pm2 daemon, which is extremely undesirable. In fact, if one sets up pm2 to automatically start on reboot, this behaviour is expected to hold true after updating Node.

Imho the daemon script generated by pm2 could possibly detect the current Node version path at startup instead of storing a constant string, and this should be enough to fix the problem.

Now, if I understand correctly it depends on how Node is installed on the system. We could start dividing in two big families:

  1. With nvm
  2. All the remaining cases (direct download, operating system package managers, etc.)

Let's consider the nvm scenario first.

As long as we can know where nvm.sh is located, it's enough to source it in the daemon script, and then derive the paths for the Node executable and the pm2 package from $NVM_BIN (a couple extra details here).

For example:

source /usr/local/nvm/nvm.sh  # (not sure yet what is the best way to localize nvm.sh)

NODE_PATH=$NVM_BIN/node
PM2_PATH=$NVM_BIN/../lib/node_modules/pm2/bin/pm2

If Node is not installed through nvm instead, other methods could be found. Before digging further, what do you think about the general approach and feasibility?

Cheers!

@pensierinmusica pensierinmusica changed the title pm2 startup not working after update pm2 startup not working after updating Node Mar 18, 2016
@Unitech
Copy link
Owner

Unitech commented Dec 6, 2016

You need to rerun pm2 startup command when the node.js version changes

#2559

@Unitech Unitech closed this as completed Dec 6, 2016
@pensierinmusica
Copy link
Author

Can you add this bit of info to the docs? If you like I can also send a PR. Thx!

@vmarchaud
Copy link
Contributor

@pensierinmusica It would be perfect if you can make a PR for this, we may update the docs ourselves but after being published on the main branch (end of december i guess)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants