- Newest
- Most votes
- Most comments
The behavior you're observing with AWS Systems Manager (SSM) Run Command and the AWS-RunShellScript document is expected. SSM is designed to wait for the entire command execution to complete, including any child processes or background tasks, before reporting the task as finished.
When you use commands like nohup or & to run processes in the background, SSM still waits for these processes to complete before considering the overall command execution as finished. This is because SSM needs to ensure that all operations initiated by the command have completed and to capture any output or errors that might be generated.
To work around this behavior and have SSM return more quickly without waiting for long-running background processes, you have a few options:
-
Use the
timeoutcommand to limit the execution time of your script. For example:timeout 5s nohup bash -c "nohup sleep 60 &"This will force the command to exit after 5 seconds, regardless of whether the background process has completed.
-
Implement a custom timeout mechanism in your script that exits the main process after a short delay, leaving the background process running.
-
Consider using AWS Systems Manager Automation instead of Run Command for more complex workflows that involve long-running processes. Automation provides more flexibility in handling asynchronous operations.
-
If you need to run long-running background processes, you might want to consider alternative approaches such as using AWS Lambda functions with longer timeout settings or EC2 instances with custom scripts that can manage long-running processes independently of SSM.
It's important to note that the execution timeout for SSM commands can be configured. The default is typically around 30 minutes, but you can adjust this using the timeoutSeconds parameter when invoking the command. However, increasing the timeout won't make SSM return faster; it will just allow longer-running commands to complete without timing out.
Sources
Run AWS Systems Manager Automation tasks synchronously from AWS Step Functions - AWS Prescriptive Guidance
How do you modify the Maintenance Window Task Invocation Execution Timeout via CDK | AWS re:Post
answered 2 years ago
Relevant content
- AWS OFFICIALUpdated 6 months ago
