pm2启动不起作用 [英] pm2 startup doesn't work

查看:1082
本文介绍了pm2启动不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功设置了我的nodejs服务器,并且在Ubuntu 15.04服务器上使用它,我的问题是我希望我的节点应用程序在服务器重新启动时能够继续运行,所以我尝试了pm2forevercrontab但是它们都不适合我,重新启动后,我需要手动启动节点应用程序.

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.

我尝试了pm2,如下所示:

pm2 startup ubuntu
pm2 start appname
pm2 save

pm2-init.sh文件:

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                                                                                                                  

那没有用,所以我尝试使用crontab,如下所示: 首先,我创建一个脚本并将其命名为starter.sh

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

然后打开crontab编辑器:

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

上述方法也没有在服务器重新启动时启动我的应用程序.

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

请提出建议,

推荐答案

pm2在重新引导期间似乎存在错误.将pm2添加到/etc/init.d中的自动启动进程后,该脚本在正常处理下可以正常工作,但在重新启动后却有些奇怪:它将pm2.dump文件清空.像这样的错误报告 ,但是到目前为止,它仍然是一个错误...

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. 编辑/etc/init.d/pm2-init.sh,并在stop()部分中注释掉"super $ PM2 dump"行
  2. 每当修改pm2进程列表时,请记住手动进行"pm2转储"

如果有人有更永久的解决方案,请让我知道...:)

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

这篇关于pm2启动不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆