簡體   English   中英

pm2 啟動不起作用

[英]pm2 startup doesn't work

我成功設置了我的 nodejs 服務器,並在 Ubuntu 15.04 服務器上使用它,我的問題是我希望我的節點應用程序在服務器重新啟動時繼續運行,所以我嘗試了pm2forevercrontab但它們都沒有為我工作,之后重新啟動我需要手動啟動節點應用程序。

我試過pm2如下:

pm2 startup ubuntu
pm2 start appname
pm2 save

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=/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

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

然后打開crontab編輯器:

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

此外,上述方法沒有在服務器重新啟動時啟動我的應用程序。

請指教,

pm2 在重啟過程中似乎有一個錯誤。 將 pm2 添加到 /etc/init.d 中自動啟動的進程后,該腳本在正常處理下工作正常,但在重新啟動時會發生一些奇怪的事情:它將 pm2.dump 文件擦除為空。 有幾個錯誤報告像這樣的,但到目前為止,它仍然是一個錯誤?

我發現的最簡單的解決方法如下:

  1. 編輯 /etc/init.d/pm2-init.sh,並注釋掉 stop() 部分中的“super $PM2 dump”行
  2. 每當你修改你的 pm2 進程列表時,記得做一個手動的“pm2 dump”

如果有人有更永久的解決方案,請告訴我... :)

對於在這里尋找 Windows 機器的人(就像我一樣),pm2 啟動僅適用於 unix 系統。

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

試試這個: https : //www.npmjs.com/package/pm2-windows-startup

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM