# InitCommand

Command to execute on the instance.

Extends: [InitElement](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.InitElement.html) › InitCommand
Obtainable from: InitCommand.argvCommand, InitCommand.shellCommand

- Source: [`aws-ec2/lib/cfn-init-elements.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-ec2/lib/cfn-init-elements.ts#L207)

## Example

TypeScript:

```ts
const handle = new ec2.InitServiceRestartHandle();
ec2.CloudFormationInit.fromElements(
  ec2.InitCommand.shellCommand('/usr/bin/custom-nginx-install.sh', { serviceRestartHandles: [handle] }),
  ec2.InitService.enable('nginx', { serviceRestartHandle: handle }),
);
```

Python:

```python
handle = ec2.InitServiceRestartHandle()
ec2.CloudFormationInit.from_elements(
    ec2.InitCommand.shell_command("/usr/bin/custom-nginx-install.sh", service_restart_handles=[handle]),
    ec2.InitService.enable("nginx", service_restart_handle=handle))
```

Java:

```java
InitServiceRestartHandle handle = new InitServiceRestartHandle();
CloudFormationInit.fromElements(InitCommand.shellCommand("/usr/bin/custom-nginx-install.sh", InitCommandOptions.builder().serviceRestartHandles(List.of(handle)).build()), InitService.enable("nginx", InitServiceOptions.builder().serviceRestartHandle(handle).build()));
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
var handle = new InitServiceRestartHandle();
CloudFormationInit.FromElements(InitCommand.ShellCommand("/usr/bin/custom-nginx-install.sh", new InitCommandOptions { ServiceRestartHandles = new [] { handle } }), InitService.Enable("nginx", new InitServiceOptions { ServiceRestartHandle = handle }));
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
handle := ec2.NewInitServiceRestartHandle()
ec2.CloudFormationInit_FromElements(ec2.InitCommand_ShellCommand(jsii.String("/usr/bin/custom-nginx-install.sh"), &InitCommandOptions{
	ServiceRestartHandles: []InitServiceRestartHandle{
		handle,
	},
}), ec2.InitService_Enable(jsii.String("nginx"), &InitServiceOptions{
	ServiceRestartHandle: handle,
}))
```

## Properties

- `elementType`: string — Returns the init element type for this element.

## Methods

- `argvCommand(argv: string[], options?: InitCommandOptions): InitCommand // chainable` (static) — Run a command from an argv array.

  You do not need to escape space characters or enclose command parameters in quotes.

- `shellCommand(shellCommand: string, options?: InitCommandOptions): InitCommand // chainable` (static) — Run a shell command.

  Remember that some characters like `&`, `|`, `;`, `>` etc. have special meaning in a shell and
  need to be preceded by a `\` if you want to treat them as part of a filename.
