Ya Basha
Ya Basha

Reputation: 1952

pm2 startup doesn't work

I setup my nodejs server successfully and I'm using it on Ubuntu 15.04 server, my issue is that I want my node applications to keep running when the server reboots so I tried pm2, forever and crontab but none of them worked for me, after rebooting I need to start the node application manually.

I tried pm2 as follow:

pm2 startup ubuntu
pm2 start appname
pm2 save

The pm2-init.sh file:

#!/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=/home/bashar/.nvm/versions/node/v4.1.1/lib/node_modules/pm2/bin/pm2                             
USER=bashar                                                                                         

export PATH=/home/bashar/.nvm/versions/node/v4.1.1/bin:$PATH                                        
export PM2_HOME="/home/bashar/.pm2"                                                                 

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"                                                                           
    export PM2_HOME                                                                                 
    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                                                                                                                  

That didn't work, so I tried to use crontab as follow: First, I create a script and named it starter.sh

#!/bin/bash
pm2 start /home/bashar/www/node/server.js

Then opened crontab editor:

crontab -e
@reboot /home/bashar/www/node/server.js

Also the above method didn't start my application on the server reboot.

Please advice,

Upvotes: 3

Views: 12856

Answers (2)

Anshuman Chatterjee
Anshuman Chatterjee

Reputation: 962

For anyone who is here looking for Windows machines(as I did), pm2 startup works with unix system only.

https://pm2.keymetrics.io/docs/usage/startup/#init-systems-supported

Try this: https://www.npmjs.com/package/pm2-windows-startup

Upvotes: 3

RonaldB
RonaldB

Reputation: 1200

pm2 seems to have a bug during the reboot. After adding pm2 to the automagically starting processes in /etc/init.d, the script works fine under normal processing, but does something weird under a reboot: it wipes the pm2.dump file empty. There are several bug reports like this one, but so far it's still a bug...

The easiest work-around that I've found is as follows:

  1. Edit /etc/init.d/pm2-init.sh, and comment out the line "super $PM2 dump" in the section for stop()
  2. Whenever you modify your pm2 process list, remember to do a manual "pm2 dump"

If anyone has a more permanent solution, please let me know... :)

Upvotes: 2

Related Questions