简体   繁体   English

在后台作业完成之前,带有 Powershell 的 SSM 发送命令不会完成

[英]SSM send-command with Powershell does not complete until background job finishes

I am using SSM send-command to start a Tomcat process on a Windows EC2 instance.我正在使用 SSM 发送命令在 Windows EC2 实例上启动 Tomcat 进程。

for example:例如:

aws ssm send-command --instance-id i-xxxx --document-name "AWS-RunPowerShellScript"
   --parameters 'commands=["startup.bat"]'

This does indeed start Tomcat but I noticed that SSM command invocation itself is stuck in progress until I eventually shut Tomcat down, at which point it immediately succeeds.这确实启动了 Tomcat 但我注意到 SSM 命令调用本身一直卡在进行中,直到我最终关闭 Tomcat 为止,此时它立即成功。

My hunch is that something about the way SSM runs Powershell blocks on any spawned child processes ( start java... in the case of Tomcat).我的预感是 SSM 在任何衍生的子进程上运行 Powershell 块的方式(在 Tomcat 的情况下start java... )。 I don't think this is unique to Tomcat or even Java, though.不过,我不认为这是 Tomcat 甚至 Java 所独有的。

Is there a way I can get Powershell to exit, leaving the child processes running in the background?有没有办法让 Powershell 退出,让子进程在后台运行?

If you want to start an independent process in PowerShell - one that stays alive even after the creating process terminates - use the Start-Process cmdlet:如果您想在 PowerShell 中启动一个独立进程- 即使在创建进程终止后仍保持活动状态 - 使用Start-Process cmdlet:

Start-Process aws -ArgumentList @'
send-command --instance-id i-xxxx --document-name AWS-RunPowerShellScript --parameters commands=["startup.bat"]
'@

Note the use of a (literal) here-string ( @'<newline>...<newline>'@ ) to make embedding quotes easier;注意使用(文字) here-string@'<newline>...<newline>'@ )使嵌入引号更容易; see the bottom section of this answer for information about PowerShell string literals.有关 PowerShell 字符串文字的信息,请参阅此答案的底部。

While -ArgumentList can accept an array of arguments, the way these are translated into a command line is broken as of PowerShell 7.0 (and will likely not get fixed due to backward compatibility concerns; see this GitHub issue ).虽然-ArgumentList可以接受 arguments数组,但从 PowerShell 7.0 开始,这些转换为命令行的方式被破坏(并且由于向后兼容性问题可能无法修复;请参阅此 ZD3B7C913CD04EBFECZ0E9EC32CB6FD58 )。

Therefore, it is best to use a single argument with embedded quoting .因此,最好使用带有嵌入式引用单个参数

Note that you should only use double quotes ( " ) as argument delimiters when calling external programs (such as aws in this case). [1]请注意,在调用外部程序(例如本例中的aws )时,您应该只使用引号 ( " )作为参数分隔符。 [1]


[1] With direct invocation of an external program, you're free to use either quoting style, because PowerShell, when it rebuilds the command line behind the scenes, uses only " to quote arguments (if needed at all). In the case of Start-Process , the -ArgumentList value is passed through to a .NET API ( System.Diagnostics.ProcessStartInfo ), where no such translation is performed. [1] 通过直接调用外部程序,您可以自由使用任何一种引用样式,因为 PowerShell 在后台重建命令行时,仅使用"来引用 arguments(如果需要的话)。在这种情况下的Start-Process-ArgumentList值被传递到 .NET API ( System.Diagnostics.ProcessStartInfo ),其中不执行此类转换。

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

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