简体   繁体   English

Windows:自动启动 PM2 和节点应用程序

[英]Windows: Auto start PM2 and node apps

At a Windows AWS server i have a NODE app and i'm using PM2 to launch the app在 Windows AWS 服务器上,我有一个 NODE 应用程序,我正在使用 PM2 启动该应用程序

I have tried the NPMs: "pm2-windows-startup" and "pm2-windows-service"我已经尝试过 NPM:“pm2-windows-startup”和“pm2-windows-service”

But after i restart my AWS instance and run但是在我重新启动我的 AWS 实例并运行之后

PM2 ls

No node app shows up in the list...列表中没有显示节点应用程序...

I followed the instructions...我按照说明操作...

  1. Installed the NPM (So PM2 auto start after reboot)安装了 NPM(所以重启后 PM2 自动启动)
  2. PM2 start myApp.js --name mySuperApp PM2 启动 myApp.js --name mySuperApp
  3. PM2 save PM2保存
  4. Reboot重启
  5. PM2 ls --> no running node apps? PM2 ls --> 没有正在运行的节点应用程序? :-( :-(

The PM2 logs dont contain any thing... PM2 日志不包含任何东西......

I have not added any ENV variables explicit (when i tried PM2 could not start any more - so i created a fresh AWS windows instance and installed every thing from scratch again...)我没有显式添加任何 ENV 变量(当我尝试 PM2 无法再启动时 - 所以我创建了一个新的 AWS windows 实例并再次从头开始安装所有东西......)

PM2 is located the default place (i have not changed any paths) PM2 位于默认位置(我没有更改任何路径)

C:\Users\Administrator\.pm2

My PM2 file contains:我的 PM2 文件包含:

2017-03-13 07:37:48: =============================================================================== 2017-03-13 07:37:48: --- New PM2 Daemon started ---------------------------------------------------- 2017-03-13 07:37:48: ===================================== ======================================== 2017-03-13 07:37:48 : --- 新的 PM2 守护进程开始了 ------------------------------------------ ----------

2017-03-13 07:37:48: Time: Mon Mar 13 2017 07:37:48 GMT+0000 (Coordinated Universal Time) 2017-03-13 07:37:48: PM2 version: 2.4.2 2017-03-13 07:37:48: Node.js version: 6.10.0 2017-03-13 07:37:48: Current arch: x64 2017-03-13 07:37:48: PM2 home: C:\Users\Administrator.pm2 2017-03-13 07:37:48: PM2 PID file: C:\Users\Administrator.pm2\pm2.pid 2017-03-13 07:37:48: RPC socket file: \.\pipe\rpc.sock 2017-03-13 07:37:48: BUS socket file: \.\pipe\pub.sock 2017-03-13 07:37:48: Application log path: C:\Users\Administrator.pm2\logs 2017-03-13 07:37:48: Process dump file: C:\Users\Administrator.pm2\dump.pm2 2017-03-13 07:37:48: Concurrent actions: 2 2017-03-13 07:37:48: SIGTERM timeout: 1600 2017-03-13 07:37:48: =============================================================================== 2017-03-13 07:37:48:时间:2017 年 3 月 13 日星期一 07:37:48 GMT+0000(协调世界时)2017-03-13 07:37:48:PM2 版本:2.4.2 2017-03 -13 07:37:48:Node.js 版本:6.10.0 2017-03-13 07:37:48:当前架构:x64 2017-03-13 07:37:48:PM2 主页:C:\Users\Administrator。 pm2 2017-03-13 07:37:48:PM2 PID 文件:C:\Users\Administrator.pm2\pm2.pid 2017-03-13 07:37:48:RPC 套接字文件:\.\pipe\rpc。 sock 2017-03-13 07:37:48:BUS 套接字文件:\.\pipe\pub.sock 2017-03-13 07:37:48:应用程序日志路径:C:\Users\Administrator.pm2\logs 2017 -03-13 07:37:48:进程转储文件:C:\Users\Administrator.pm2\dump.pm2 2017-03-13 07:37:48:并发操作:2 2017-03-13 07:37: 48:SIGTERM 超时:1600 2017-03-13 07:37:48:================================= ==============================================

2017-03-13 07:37:48: Starting execution sequence in -fork mode- for app name:mySuperApp id:0 2017-03-13 07:37:48: App name:mySuperApp id:0 online 2017-03-13 07:40:45: =============================================================================== 2017-03-13 07:37:48:在 -fork 模式下开始执行序列 - 应用名称:mySuperApp id:0 2017-03-13 07:37:48:应用名称:mySuperApp id:0 在线 2017-03- 13 07:40:45: =========================================== ====================================

2017-03-13 07:40:45: --- New PM2 Daemon started ---------------------------------------------------- 2017-03-13 07:40:45: Time: Mon Mar 13 2017 07:40:45 GMT+0000 (Coordinated Universal Time) 2017-03-13 07:40:45: PM2 version: 2.4.2 2017-03-13 07:40:45: Node.js version: 6.10.0 2017-03-13 07:40:45: Current arch: x64 2017-03-13 07:40:45: PM2 home: C:\Users\Administrator.pm2 2017-03-13 07:40:45: PM2 PID file: C:\Users\Administrator.pm2\pm2.pid 2017-03-13 07:40:45: RPC socket file: \.\pipe\rpc.sock 2017-03-13 07:40:45: BUS socket file: \.\pipe\pub.sock 2017-03-13 07:40:45: Application log path: C:\Users\Administrator.pm2\logs 2017-03-13 07:40:45: Process dump file: C:\Users\Administrator.pm2\dump.pm2 2017-03-13 07:40:45: Concurrent actions: 2 2017-03-13 07:40:45: SIGTERM timeout: 1600 2017-03-13 07:40:45: =============================================================================== 2017-03-13 07:40:45: --- 新的 PM2 守护进程启动了 ------------------------------ ------------------ 2017-03-13 07:40:45:时间:2017 年 3 月 13 日星期一 07:40:45 GMT+0000(协调世界时) 2017-03-13 07:40:45:PM2 版本:2.4.2 2017-03-13 07:40:45:Node.js 版本:6.10.0 2017-03-13 07:40:45:当前拱门:x64 2017 -03-13 07:40:45: PM2 主页:C:\Users\Administrator.pm2 2017-03-13 07:40:45: PM2 PID 文件:C:\Users\Administrator.pm2\pm2.pid 2017- 03-13 07:40:45:RPC 套接字文件:\.\pipe\rpc.sock 2017-03-13 07:40:45:BUS 套接字文件:\.\pipe\pub.sock 2017-03-13 07 :40:45:应用程序日志路径:C:\Users\Administrator.pm2\logs 2017-03-13 07:40:45:进程转储文件:C:\Users\Administrator.pm2\dump.pm2 2017-03- 13 07:40:45:并发操作:2 2017-03-13 07:40:45:SIGTERM 超时:1600 2017-03-13 07:40:45:============= ================================================ ================

My PM2 DUMB file contains:我的 PM2 DUMB 文件包含:

[ { "exec_mode": "fork_mode", "watch": false, "treekill": true, "autorestart": true, "automation": true, "pmx": true, "vizion": true, "name": "mySuperApp", "node_args": [], "pm_exec_path": "c:\mypath\mySuperApp\server.js", "env": { "windir": "C:\Windows", "USERPROFILE": "C:\Users\Administrator", "USERNAME": "Administrator", "USERDOMAIN_ROAMINGPROFILE": "EC2AMAZ-REBQJDK", "USERDOMAIN": "EC2AMAZ-REBQJDK", "TMP": "C:\Users\ADMINI~1\AppData\Local\Temp\2", "TEMP": "C:\Users\ADMINI~1\AppData\Local\Temp\2", "SystemRoot": "C:\Windows", "SystemDrive": "C:", "SESSIONNAME": "RDP-Tcp#1", "PUBLIC": "C:\Users\Public", "PSModulePath": "C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files (x86)\AWS Tools\PowerShell\", "PROMPT": "$P$G", "ProgramW6432": "C:\Program Files", "ProgramFiles(x86)": "C:\Program Files (x86)", "ProgramFiles": "C:\Program Files", "ProgramData": "C:\ProgramData", "PROCESSOR_REVISION": "3f02", "PRO [{“exec_mode”:“fork_mode”,“watch”:false,“treekill”:true,“autorestart”:true,“automation”:true,“pmx”:true,“vizion”:true,“name”: "mySuperApp", "node_args": [], "pm_exec_path": "c:\mypath\mySuperApp\server.js", "env": { "windir": "C:\Windows", "USERPROFILE": "C :\Users\Administrator", "USERNAME": "Administrator", "USERDOMAIN_ROAMINGPROFILE": "EC2AMAZ-REBQJDK", "USERDOMAIN": "EC2AMAZ-REBQJDK", "TMP": "C:\Users\ADMINI~1\AppData \Local\Temp\2", "TEMP": "C:\Users\ADMINI~1\AppData\Local\Temp\2", "SystemRoot": "C:\Windows", "SystemDrive": "C:" , "SESSIONNAME": "RDP-Tcp#1", "PUBLIC": "C:\Users\Public", "PSModulePath": "C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\ v1.0\Modules;C:\Program Files (x86)\AWS Tools\PowerShell\", "PROMPT": "$P$G", "ProgramW6432": "C:\Program Files", "ProgramFiles(x86) ": "C:\Program Files (x86)", "ProgramFiles": "C:\Program Files", "ProgramData": "C:\ProgramData", "PROCESSOR_REVISION": "3f02", "PRO CESSOR_LEVEL": "6", "PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 63 Stepping 2, GenuineIntel", "PROCESSOR_ARCHITECTURE": "AMD64", "PM2_USAGE": "CLI", "PM2_INTERACTOR_PROCESSING": "true", "PATHEXT": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JSE;.WSF;.WSH;.MSC", "Path": "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Amazon\cfn-bootstrap\;C:\Program Files\nodejs\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\AppData\Roaming\npm", "OS": "Windows_NT", "NUMBER_OF_PROCESSORS": "1", "LOGONSERVER": "\\EC2AMAZ-REBQJDK", "LOCALAPPDATA": "C:\Users\Administrator\AppData\Local", "HOMEPATH": "\Users\Administrator", "HOMEDRIVE": "C:", "ComSpec": "C:\Windows\system32\cmd.exe", "COMPUTERNAME": "EC2AMAZ-REBQJDK", "CommonProgramW6432": "C:\Program Files\Common Files", "CommonProgramFiles( CESSOR_LEVEL": "6", "PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 63 Stepping 2, GenuineIntel", "PROCESSOR_ARCHITECTURE": "AMD64", "PM2_USAGE": "CLI", "PM2_INTERACTOR_PROCESSING": "true", "PATHEXT" : ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JSE;.WSF;.WSH;.MSC", "路径": "C:\Windows\system32;C:\Windows; C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Amazon\cfn-bootstrap\;C:\Program Files\nodejs\85038\855User3;8681 Administrator\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\AppData\Roaming\npm", "OS": "Windows_NT", "NUMBER_OF_PROCESSORS": "1", "LOGONSERVER": "\\EC2AMAZ-REBQJDK ", "LOCALAPPDATA": "C:\Users\Administrator\AppData\Local", "HOMEPATH": "\Users\Administrator", "HOMEDRIVE": "C:", "ComSpec": "C:\Windows\system32 \cmd.exe", "COMPUTERNAME": "EC2AMAZ-REBQJDK", "CommonProgramW6432": "C:\Program Files\Common Files", "CommonProgramFiles( x86)": "C:\Program Files (x86)\Common Files", "CommonProgramFiles": "C:\Program Files\Common Files", "CLIENTNAME": "THESILVERFOX", "APPDATA": "C:\Users\Administrator\AppData\Roaming", "ALLUSERSPROFILE": "C:\ProgramData", "PM2_HOME": "C:\Users\Administrator\.pm2", "mySuperApp": {} }, "pm_cwd": "c:\mypath\mySuperApp", "exec_interpreter": "node", "pm_out_log_path": "C:\Users\Administrator\.pm2\logs\mySuperApp-out-0.log", "pm_err_log_path": "C:\Users\Administrator\.pm2\logs\mySuperApp-error-0.log", "pm_pid_path": "C:\Users\Administrator\.pm2\pids\mySuperApp-0.pid", "km_link": false, "NODE_APP_INSTANCE": 0, "vizion_running": false, "windir": "C:\Windows", "USERPROFILE": "C:\Users\Administrator", "USERNAME": "Administrator", "USERDOMAIN_ROAMINGPROFILE": "EC2AMAZ-REBQJDK", "USERDOMAIN": "EC2AMAZ-REBQJDK", "TMP": "C:\Users\ADMINI~1\AppData\Local\Temp\2", "TEMP": "C:\Users\ADMINI~1\AppData\Local\Temp\2", "SystemRoot": "C:\Windows", "SystemDrive": "C:", "SESSIONNAME": "RDP-Tcp#1", "PUBLIC": "C:\ x86)": "C:\Program Files (x86)\Common Files", "CommonProgramFiles": "C:\Program Files\Common Files", "CLIENTNAME": "THESILVERFOX", "APPDATA": "C:\Users \Administrator\AppData\Roaming", "ALLUSERSPROFILE": "C:\ProgramData", "PM2_HOME": "C:\Users\Administrator\.pm2", "mySuperApp": {} }, "pm_cwd": "c: \mypath\mySuperApp", "exec_interpreter": "node", "pm_out_log_path": "C:\Users\Administrator\.pm2\logs\mySuperApp-out-0.log", "pm_err_log_path": "C:\Users\ Administrator\.pm2\logs\mySuperApp-error-0.log", "pm_pid_path": "C:\Users\Administrator\.pm2\pids\mySuperApp-0.pid", "km_link": false, "NODE_APP_INSTANCE": 0, "vizion_running": false, "windir": "C:\Windows", "USERPROFILE": "C:\Users\Administrator", "USERNAME": "Administrator", "USERDOMAIN_ROAMINGPROFILE": "EC2AMAZ-REBQJDK", “USERDOMAIN”:“EC2AMAZ-REBQJDK”,“TMP”:“C:\Users\ADMINI~1\AppData\Local\Temp\2”,“TEMP”:“C:\Users\ADMINI~1\AppData\Local \Temp\2", "SystemRoot": "C:\Windows", "SystemDrive": "C:", "SESSIONNAME": "RDP-Tcp#1", "PUBLIC": "C:\ Users\Public", "PSModulePath": "C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files (x86)\AWS Tools\PowerShell\", "PROMPT": "$P$G", "ProgramW6432": "C:\Program Files", "ProgramFiles(x86)": "C:\Program Files (x86)", "ProgramFiles": "C:\Program Files", "ProgramData": "C:\ProgramData", "PROCESSOR_REVISION": "3f02", "PROCESSOR_LEVEL": "6", "PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 63 Stepping 2, GenuineIntel", "PROCESSOR_ARCHITECTURE": "AMD64", "PM2_USAGE": "CLI", "PM2_INTERACTOR_PROCESSING": "true", "PATHEXT": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JSE;.WSF;.WSH;.MSC", "Path": "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Amazon\cfn-bootstrap\;C:\Program Files\nodejs\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;881 Users\Public", "PSModulePath": "C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files (x86)\AWS Tools\PowerShell\" , "PROMPT": "$P$G", "ProgramW6432": "C:\Program Files", "ProgramFiles(x86)": "C:\Program Files (x86)", "ProgramFiles": "C:\ Program Files", "ProgramData": "C:\ProgramData", "PROCESSOR_REVISION": "3f02", "PROCESSOR_LEVEL": "6", "PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 63 Stepping 2, GenuineIntel", "PROCESSOR_ARCHITECTURE" : "AMD64", "PM2_USAGE": "CLI", "PM2_INTERACTOR_PROCESSING": "true", "PATHEXT": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JSE;.WSF; .WSH;.MSC", "路径": "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ Program Files\Amazon\cfn-bootstrap\;C:\Program Files\nodejs\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;881 03756586388:\Users\Administrator\AppData\Roaming\npm", "OS": "Windows_NT", "NUMBER_OF_PROCESSORS": "1", "LOGONSERVER": "\\EC2AMAZ-REBQJDK", "LOCALAPPDATA": "C:\Users\Administrator\AppData\Local", "HOMEPATH": "\Users\Administrator", "HOMEDRIVE": "C:", "ComSpec": "C:\Windows\system32\cmd.exe", "COMPUTERNAME": "EC2AMAZ-REBQJDK", "CommonProgramW6432": "C:\Program Files\Common Files", "CommonProgramFiles(x86)": "C:\Program Files (x86)\Common Files", "CommonProgramFiles": "C:\Program Files\Common Files", "CLIENTNAME": "THESILVERFOX", "APPDATA": "C:\Users\Administrator\AppData\Roaming", "ALLUSERSPROFILE": "C:\ProgramData", "PM2_HOME": "C:\Users\Administrator\.pm2", "status": "online", "pm_uptime": 1489390668484, "axm_actions": [], "axm_monitor": { "Loop delay": { "alert": {}, "agg_type": "avg", "value": "36.91ms" } }, "axm_options": { "default_actions": true, "transactions": false, "http": false, "http_latency": 200, "http_code": 500, "ignore_routes": [], "profiling": true, "errors": true, "a 03756586388:\Users\Administrator\AppData\Roaming\npm", "OS": "Windows_NT", "NUMBER_OF_PROCESSORS": "1", "LOGONSERVER": "\\EC2AMAZ-REBQJDK", "LOCALAPPDATA": "C:\ Users\Administrator\AppData\Local", "HOMEPATH": "\Users\Administrator", "HOMEDRIVE": "C:", "ComSpec": "C:\Windows\system32\cmd.exe", "COMPUTERNAME": “EC2AMAZ-REBQJDK”,“CommonProgramW6432”:“C:\ Program Files\Common Files", "CLIENTNAME": "THESILVERFOX", "APPDATA": "C:\Users\Administrator\AppData\Roaming", "ALLUSERSPROFILE": "C:\ProgramData", "PM2_HOME": "C: \Users\Administrator\.pm2", "status": "online", "pm_uptime": 1489390668484, "axm_actions": [], "axm_monitor": { "Loop delay": { "alert": {}, "agg_type “:“平均”,“价值”:“36.91ms”}},“axm_options”:{“default_actions”:true,“交易”:false,“http”:false,“http_latency”:200,“http_code”: 500,“ignore_routes”:[],“分析”:真,“错误”:真,“一个lert_enabled": true, "custom_probes": true, .network": false, "ports": false, "ignoreFilter": { "method": [ "OPTIONS" ], "url": [] }, "excludedHooks": [], "module_conf": {}, "module_name": "mySuperApp", "module_version": "2.4.2", "pmx_version": "1.0.3", "error": true }, "axm_dynamic": {}, "created_at": 1489390668484, "restart_time": 0, "unstable_restarts": 0, "versioning": null, "node_version": "6.10.0" } ] lert_enabled": true, "custom_probes": true, .network": false, "ports": false, "ignoreFilter": { "method": [ "OPTIONS" ], "url": [] }, "excludedHooks": [], "module_conf": {}, "module_name": "mySuperApp", "module_version": "2.4.2", "pmx_version": "1.0.3", "error": true }, "axm_dynamic": { }, "created_at": 1489390668484, "restart_time": 0, "unstable_restarts": 0, "versioning": null, "node_version": "6.10.0" } ]

UPDATE 16 FEB 2020: 2020 年 2 月 16 日更新:

If it is important to you that PM2 automatically starts up without you logging into the machine (after reboot) please follow my new set of instructions instead of the old ones.如果您认为 PM2 无需您登录机器​​(重启后)即可自动启动对您很重要,请按照我的新说明而不是旧说明进行操作。

New instructions (recommended):新说明(推荐):

Prerequisites (part # 1):先决条件(第 1 部分):

First, I have installed NPM in a location which is available to all users.首先,我将NPM安装在所有用户都可以使用的位置。 Depending on your use-case(s) it might not be necessary.根据您的用例,可能没有必要。 But if you like to change your default location of NPM - you should do it first (before continuing).但是,如果您想更改NPM的默认位置 - 您应该先执行此操作(在继续之前)。 Here is how you change it to the location (in terminal as administrator): C:\NodeJS\npm :以下是将其更改为位置的方法(以管理员身份在终端中): C:\NodeJS\npm

npm config set prefix "C:\\NodeJS\\npm"
npm config set cache "C:\\NodeJS\\npm-cache"
npm config set temp "C:\\NodeJS\\temp"
npm config ls -l (this will list all NPM settings -> look for the 3 lines/changes marked as `overriden`)

Prerequisites (part # 2):先决条件(第 2 部分):

  1. Add and set PM2_HOME in System environments (not user environments).System environments (不是用户环境)中添加和设置PM2_HOME Like: PM2_HOME = C:\NodeJS\npm像: PM2_HOME = C:\NodeJS\npm
  2. Add C:\NodeJS\npm to the existing system PATH variable (Then you are sure it will work - there has been some issues reported that PM2_HOME not always working).C:\NodeJS\npm添加到现有的系统 PATH 变量中(然后你确定它会工作 - 报告了一些问题PM2_HOME并不总是工作)。
  3. Close all terminals and open them again (as administrator).关闭所有终端并再次打开它们(以管理员身份)。 Your terminal windows will now be aware of your environment changes.您的终端窗口现在将知道您的环境更改。

Prerequisites (part # 3):先决条件(第 3 部分):

  1. npm install pm2 -g npm 安装 pm2 -g
  2. npm i pm2-windows-service -g npm i pm2-windows-service -g
  3. npm install -g npm-check-updates npm install -g npm-check-updates

Currently there is a bug in a module which the package pm2-windows-service uses - so lets fix this as well, please follow the steps below:目前, pm2-windows-service包使用的模块中有一个错误 - 所以让我们也修复这个问题,请按照以下步骤操作:

  1. In terminal cd into: C:\NodeJS\npm\node_modules\pm2-windows-service在终端cd进入: C:\NodeJS\npm\node_modules\pm2-windows-service
  2. ncu inquirer this only outputs the existing and the newest available version of the inquirer module we need to update, currently: version: 1.1.2 --> 7.0.4 . ncu inquirer这仅输出我们需要更新的inquirer器模块的现有和最新可用版本,当前:版本: 1.1.2 --> 7.0.4
  3. ncu inquirer -u this will update your packages.json file. ncu inquirer -u这将更新您的 packages.json 文件。
  4. npm install this will download and update the inquirer module (please be aware if you don't use specific version syntax in your packages.json file or you have made manually changes --> other modules would be updated as well. npm install这将下载和更新inquirer器模块(请注意,如果您没有在您的packages.json文件中使用特定的版本语法,或者您手动进行了更改——> 其他模块也将被更新。

Install and setup PM2 (as a service) to automatically startup after reboot:安装并设置 PM2(作为服务)以在重启后自动启动:

  1. In terminal cd into: C:\NodeJS\npm\node_modules\pm2-windows-service在终端cd进入: C:\NodeJS\npm\node_modules\pm2-windows-service
  2. pm2-service-install -n PM2_STARTUP_SCRIPT ( PM2_STARTUP_SCRIPT will be the "Display name" of the Windows service. Change it to what you prefer and hit ENTER .) pm2-service-install -n PM2_STARTUP_SCRIPTPM2_STARTUP_SCRIPT将是 Windows 服务的“显示名称”。将其更改为您喜欢的名称,然后按ENTER 。)
  3. Perform environment setup (recommended)?执行环境设置(推荐)? Yes
  4. Set PM2_HOME?设置 PM2_HOME? No (No need - You have set it already) No (不需要 - 您已经设置好了)
  5. Set PM2_SERVICE_SCRIPTS (the list of start-up scripts for pm2)?设置 PM2_SERVICE_SCRIPTS(pm2 的启动脚本列表)? Yes
  6. Set the list of startup scripts/files (semi-colon separated json config files or js files) ENTER (when nothing is entered - it will default to use PM2's dump.pm2 file - which is created when you run PM2 -f save , I will return and explain this later on).设置启动脚本/文件列表(分号分隔的 json 配置文件或 js 文件) ENTER (当没有输入时 - 它会默认使用 PM2 的dump.pm2文件 - 当你运行PM2 -f save时创建,我稍后将返回并解释这一点)。
  7. Set PM2_SERVICE_PM2_DIR (the location of the global pm2 to use with the service)?设置 PM2_SERVICE_PM2_DIR(与服务一起使用的全局 pm2 的位置)? Yes

  8. Specify the directory containing the pm2 version to be used by the service?指定包含服务要使用的 pm2 版本的目录? ENTER

PM2 service installed and started. PM2 服务已安装并启动。

  1. Open Services in Windows and change the service to run as Administrator (or your preferred role).在 Windows 中打开服务并将服务更改为以管理员(或您的首选角色)身份运行。

Setup the app(s) you like PM2 to startup - when shutdown or after a reboot:设置您喜欢 PM2 的应用程序以启动 - 关闭或重新启动后:

  1. pm2 start myApp.js --name mySuperApp
  2. pm2 -f save
  3. reboot or if you use AWS (or any other cloud provider).重新启动,或者如果您使用 AWS(或任何其他云提供商)。 Reboot your instance - wait 5 minutes and then log into the machine and execute pm2 ls and check your app has been up and running for ~ 5 min (and not only few secs because you just logged in).重新启动您的实例 - 等待 5 分钟,然后登录机器并执行pm2 ls并检查您的应用程序是否已启动并运行约 5 分钟(不仅是几秒钟,因为您刚刚登录)。

Uninstall and cleanup "pm2-windows-startup" from your registry (if you switch from my "old instruction" to the new ones):从您的注册表中卸载并清理“pm2-windows-startup”(如果您从我的“旧指令”切换到新指令):

  1. npm uninstall pm2-windows-startup -g npm 卸载 pm2-windows-startup -g
  2. Delete the PM2 key from registry like in the picture below:从注册表中删除PM2键,如下图所示:

在此处输入图像描述

Old instructions (not recommended):旧指令(不推荐):

My old answer below is still working - but PM2 doesn't startup unless you log into the machine because it is loading PM2 from registry and doesn't run it as a service.我在下面的旧答案仍然有效 - 但除非您登录机器​​,否则 PM2 不会启动,因为它正在从注册表加载 PM2 并且不会将其作为服务运行。

I don't know why - but after several attempts this worked out (at a fresh installed AWS Windows 2016 BASE instance)我不知道为什么 - 但经过几次尝试,这解决了(在新安装的 AWS Windows 2016 BASE 实例上)

  1. npm install pm2 -g npm 安装 pm2 -g
  2. npm install pm2-windows-startup -g npm install pm2-windows-startup -g
  3. pm2-startup install pm2-启动安装
  4. pm2 start myApp.js --name mySuperApp pm2 启动 myApp.js --name mySuperApp
  5. pm2 save pm2 保存
  6. reboot重启
  7. pm2 ls下午2点

UPDATE 08/06/2022 2022 年 8 月 6 日更新

I found out that npm i -g pm2 is global install per user .我发现npm i -g pm2是 global install per user
it means that if another user logged in into the server, he won't have pm2 install - because -g is not fully global as we want it to be.这意味着如果另一个用户登录到服务器,他将不会安装 pm2 - 因为-g并不像我们希望的那样完全全局。

so the solution for those who wants it fully globall:所以对于那些想要它完全全球化的人的解决方案:

Configure pm2配置pm2

  1. create folder c:\etc创建文件夹c:\etc
  2. install pm2 in this folder cd c:\etc npm i pm2在此文件夹中安装 pm2 cd c:\etc npm i pm2
  3. add C:\etc\node_modules\.bin to the System variable PATHC:\etc\node_modules\.bin添加到系统变量PATH
  4. type pm2 to initialize and create the .pm2 folder键入pm2以初始化并创建.pm2文件夹
  5. copy it from C:\Users\USER\.pm2 to C:\etc\.pm2将其从C:\Users\USER\.pm2C:\etc\.pm2
  6. set a new System Variable (not user level) name: PM2_HOME value: c:\etc\.pm2设置一个新的系统变量(不是用户级别)名称: PM2_HOME值: c:\etc\.pm2

Configure pm2配置pm2

1. `npm i -g pm2` 2. type `pm2` to initialize and create the `.pm2` folder 3. copy it from `C:\Users\USER\.pm2` to `C:\etc\.pm2` 3. set a new System Variable (not user level) name: `PM2_HOME` value: `c:\etc\.pm2` 1. `npm i -g pm2` 2. 输入 `pm2` 初始化并创建 `.pm2` 文件夹 3. 将其从 `C:\Users\USER\.pm2` 复制到 `C:\etc\.pm2 ` 3. 设置一个新的系统变量(不是用户级别)名称:`PM2_HOME` 值:`c:\etc\.pm2`

Runnning your app with pm2使用 pm2 运行您的应用程序

  1. run your pm2 app.运行您的 pm2 应用程序。 ie: pm2 start app.js --name=MY_API .即: pm2 start app.js --name=MY_API
  2. pm2 save to create a dump of the current apps running. pm2 save以创建当前正在运行的应用程序的转储。

Testing app测试应用

  1. to test everything is working, try: pm2 kill and then pm2 resurrect (app should be running, check with pm2 status )要测试一切正常,请尝试: pm2 kill然后pm2 resurrect (应用程序应该正在运行,检查pm2 status

Run at startup在启动时运行

now we need to perform the resurrect command at startup, so:现在我们需要在启动时执行 resurrect 命令,所以:

  1. npm install -g @innomizetech/pm2-windows-service
  2. pm2-service-install -n PM2 --unattended

thats it.而已。

pm2-windows-startup works great if you're okay with the fact that it is launched on login. pm2-windows-startup如果您对它在登录时启动这一事实感到满意,那么它的效果很好。 If you have a reboot on a server though (say Windows Update) you are out of luck.如果您在服务器上重新启动(例如 Windows 更新),那么您就不走运了。

pm2-windows-service did work for me, using @innomizetech fork, but I've had some issues, probably due to the user or its setup, or something else. pm2-windows-service确实为我工作,使用@innomizetech fork,但我遇到了一些问题,可能是由于用户或其设置或其他原因。 Basically the service would start an old version of the saved process list, even though I tried pm2 delete all , pm2 start ecosystem.config.js , pm2 save .基本上该服务会启动一个旧版本的已保存进程列表,即使我尝试过pm2 delete allpm2 start ecosystem.config.jspm2 save

I resorted to a very simple home-made solution:我采用了一个非常简单的自制解决方案:

  1. Create a file pm2-resurrect.sh in C:\ , which contains the single line pm2 resurrect .C:\创建一个文件pm2-resurrect.sh ,其中包含单行pm2 resurrect
  2. In Task Scheduler, add a new task that is run on boot, and select that it is run whether the user is logged in or not.在任务计划程序中,添加一个在启动时运行的新任务,并选择无论用户是否登录都运行它。 You need to type the user password at this point which is saved for successive runs.此时您需要输入用户密码,该密码已保存以供后续运行。
  3. Set the task command to execute the file C:\pm2-resurrect.sh .设置任务命令执行文件C:\pm2-resurrect.sh

In my case I have Git for Windows which comes with bash, which opens the file and executes it.在我的例子中,我有 Git for Windows,它带有 bash,它打开文件并执行它。 I didn't test it but I guess you could have a .cmd file with the same content.我没有测试它,但我想你可以有一个具有相同内容的.cmd文件。

I tried all of the above with no luck.我尝试了以上所有方法都没有运气。 What worked for me:什么对我有用:

  1. create a bat file the pm2 resurrect command创建一个bat文件pm2 resurrect命令
  2. create a shortcut of that file创建该文件的快捷方式
  3. pasting that shortcut on the startup folder (win+R , shell:startup)将该快捷方式粘贴到启动文件夹(win+R,shell:startup)

easiest way & it works great hope it will help some最简单的方法,效果很好,希望对某些人有所帮助

  • I have created one batch file *.bat in same folder我在同一个文件夹中创建了一个批处理文件 *.bat
  • %windir%\system32\CMD.exe /k "start pm2 start myApp.js --name mySuperApp"
  • created windows "Task Scheduler" on startup works great...在启动时创建的窗口“任务计划程序”效果很好......

See pm2-installer请参阅pm2 安装程序

Readme is also make it clear how to use it. Readme 也清楚地说明了如何使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM