首页 > 解决方案 > AWS 用户数据无法在 Windows Server 2019 上运行

问题描述

当前设置:

在 AWS 中手动启动带有节点应用程序的 Windows Server 2019 实例。下面的命令在这个实例上工作正常

Pm2 delete myapp
Pm2 start

问题 :

我们已经创建了上述实例的 AMI,并使用 cloudformation 模板启动它。在 cloudformation 模板的 userdata 中,我们对 nodejs 的配置文件进行了一些更改,并使用上述命令重新启动了节点应用程序。

输出:

使用 cloduformation userdata 成功修改配置文件

错误:

上述命令(Pm2 delete myapp 和 Pm2 start)在执行 userdata 时失败。下面是用户数据日志。

The errors from user scripts: pm2 : The term 'pm2' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Windows\TEMP\UserScript.ps1:15 char:1
+ pm2 delete RenderWorker
+ ~~~
    + CategoryInfo          : ObjectNotFound: (pm2:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
pm2 : The term 'pm2' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Windows\TEMP\UserScript.ps1:16 char:1
+ pm2 start
+ ~~~
    + CategoryInfo          : ObjectNotFound: (pm2:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

其他发现:

Pm2 命令可用于新启动的 ec2 实例。

C:\Users\Administrator>pm2
usage: pm2 [options] <command>

pm2 -h, --help             all available commands and options
pm2 examples               display pm2 usage examples
pm2 <command> -h           help on a specific command

Access pm2 files in ~/.pm2

cloudofmation中的用户数据:

 UserData:
    Fn::Base64: !Sub |
      <powershell>
      $config = '{
      "MyENV": "${MyENV}",
      "UR1L": "${URL1}",
      "URL2": "${URL2}",
      "BUCKET": "${MyBucket}",
      "PATH": "${Mypath}"
      }'
      echo $config
      $config | Set-Content C:\Projects\myapp\settings\config.json
      cd 'C:\Projects\myapp'
      pm2 delete RenderWorker
      pm2 start
      </powershell>
      <runAsLocalSystem>true</runAsLocalSystem>
      <persist>true</persist>

标签: node.jsamazon-web-servicesamazon-ec2amazon-cloudformationpm2

解决方案


我怀疑您是否可以pm2直接这样运行,因为pm2SYSTEM 路径下不存在路径。

您可能需要按如下方式包含它的完整路径才能运行该命令。

例如,您的pm2居住C:\etc\地如下,您需要包括以下内容:

C:\etc\.pm2 delete RenderWorker
C:\etc\.pm2 start

或者如果您熟悉路径,则可以将其作为环境变量。


推荐阅读