Menu
Amazon Elastic Compute Cloud
User Guide for Linux Instances

Running Shell Scripts Using Amazon EC2 Run Command

You can use the AWS-RunShellScript document to run shell scripts on your instances.

Note

This procedure does not include information about how to configure Run Command for Amazon SNS notifications. To learn more about the Amazon SNS notification fields on the Command History page and how to execute commands that return notifications, see Getting Amazon SNS Notifications When a Command Changes Status.

To run shell scripts using Run Command

  1. Open the Amazon EC2 console and choose Command History in the navigation pane.

  2. Choose Run a Command.

  3. In the Command document list, choose AWS-RunShellScript.

  4. Choose Select target instances to select the instances where you want the command to run. If you do not see a complete list of instances, the missing instances might not be configured properly for Run Command. For more information, see Amazon EC2 Run Command Prerequisites.

  5. Enter a script or the path to a script in the Commands field.

  6. (Optional) In the Working Directory field, enter the path to the folder on your EC2 instances where you want to run the command.

  7. (Optional) In the Execution Timeout field, enter the number of seconds the Amazon SSM agent will attempt to run the command before it times out and fails.

  8. (Optional) In the Comment field, enter information you want to provide about this command. Comments are stored in the log file and appear in the Command Invocation List in the console.

    Tip

    We recommend that you enter specific comments about each command you run. The list of commands that you send can grow quickly, and the Comment field can help you identify commands that you want to monitor.

  9. In the Timeout (seconds) field, enter the number of seconds Run Command should attempt to reach instances before an instance is considered unreachable and the command execution fails. The minimum is 30 seconds. The maximum is 30 days. The default value is 10 minutes.

  10. In the Amazon S3 bucket field enter the name of an Amazon S3 bucket where you want to store the output of the command.

    Important

    The Run Command Output page in the Amazon EC2 console truncates output after 2500 characters. Configure an Amazon S3 bucket before executing commands using Run Command. If your command output was longer than 2500 characters, you can view the full output in your Amazon S3 bucket. For more information, see Create a Bucket.

  11. In the S3 key prefix, field enter the name of a subfolder in the Amazon S3 bucket. This subfolder can help you organize Run Command output.

Note

The section called AWS Command Line Interface command displays a usable CLI script that is generated based on the parameters you entered.

After you execute a command using Run Command, the system returns you to the commands list.

Important

The Amazon EC2 console truncates all command output beyond 2500 characters. If your command output was longer than 2500 characters, you can view the full output in your S3 bucket.

Canceling a Command

You can attempt to cancel a command as long as the service shows that it is in either a Pending or Executing state. However, even if a command is still in one of these states, we cannot guarantee that the command will be terminated and the underlying process stopped.

To cancel a command using the console

  1. Open the Amazon EC2 console and choose Command History in the navigation pane.

  2. Select the command invocation that you want to cancel.

  3. Choose Actions and then choose Cancel command.

To cancel a command using the AWS CLI

Use the following command.

aws ssm cancel-command --command-id "command  ID" --instance-ids "instance ID"

For information about the status of a cancelled command, see Monitoring Commands.

View Command Output

Use the following procedure to view the results of command execution in the EC2 console.

To view command output

  1. In the Amazon EC2 console, select a command in the list.

  2. Choose the Output tab.

  3. Choose View Output.

    Accessing Run Command output from Amazon EC2 console
  4. The command output page shows the results of your command execution.

    Example of Run Command output

For information about how to run commands using the AWS CLI, see the SSM CLI Reference.