pm2 startup doesn't work
Asked Answered
S

2

2

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,

Siobhan answered 29/9, 2015 at 8:24 Comment(2)
I've managed to get my system starting properly - #32769802 - though I'm not sure if ubuntu 15.04 is quite the sameLadylove
No that didn't work with me, you didn't do other than the suggested steps to start pm2 job. The version of pm2 already 0.15.7Siobhan
Q
2

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... :)

Quietly answered 15/1, 2016 at 21:42 Comment(6)
Gave this a shot and can't seem to get it working :(Ovum
Did you find and comment out the line that dumps the pm2 configuration? This may be particular to Ubuntu...Quietly
how do we manually do "pm2 dump".secondly even after commenting super $PM2 dump after restart issues still persists ! btw iam using ubunutu 14.04 ltsDetta
great it worked for me now the issue was with drive being not mount after rebootDetta
@RizwanPatel I've entered "pm2 dump" on the command line after making changes to the process list. Not sure if I can help you with a non-mounting drive, that is hopefully unrelated to pm2. Good luck though!Quietly
no i use gnome to sort mounting issue but your provided solutions works as charms in context with current thread in question. thanksDetta
E
2

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

Euryale answered 14/1, 2021 at 12:49 Comment(1)
works like a charm :)Tricia

© 2022 - 2024 — McMap. All rights reserved.