SSM Run CommandのターゲットのEC2インスタンス上でCommand IDを取得してみた

SSM Run CommandのターゲットのEC2インスタンス上でCommand IDを取得してみた

SSM AgentのログファイルからPPIDで検索して抽出する
Clock Icon2023.11.03

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

SSM Run CommandのターゲットのEC2インスタンス上でCommand IDを取得したい

こんにちは、のんピ(@non____97)です。

皆さんはSSM Run CommandのターゲットのEC2インスタンス上でCommand IDを取得したいなと思ったことはありますか? 私はあります。

例えば、SSM Run Command実行中に、そのRun Commandよりも後から実行されたRun Commandを検索したい時に使いたいところです。

自身のCommand IDが分からない場合は、Run Commandで渡すコマンド内で「現在時刻よりも新しいRun Commandの実行はどれか」という処理を行うことになります。

個人的にはRun Command内で実際にコマンドを受け付けられた時間から検索するのではなく、Run Command自体が実行された時間をベースに検索したいです。また、前者の場合は、同時実行された場合に自身のRun Commandなのか、別のRun Commandなのかの判断をすることが難しいように思えます。

そのためにはターゲットなったEC2インスタンスが、どのRun Commandで実行されたのかを正確に把握する必要があります。

SSM Run Commandを実行してもCommand IDの環境変数が埋め込まれるということはありません。

これの解決方法としては、「SSM AgentのログファイルからPPIDで検索して抽出する」という方法になります。

実際にやってみます。

いきなりまとめ

  • SSM AgentのログファイルからPPIDで検索して抽出できる
  • sleepなどで数秒待機しなければ、Run Commandを受け付けたログが出力されないので注意

やってみた

実際にやってみます。

ターゲットとするEC2インスタンスの情報は以下です。

  • AMI名 : RHEL-9.2.0_HVM-20230503-x86_64-41-Hourly2-GP2
  • AMI ID : ami-026ebd4cfe2c043b2 (us-east-1)

まず、SSM Run Command実行時のSSM Agentのログを確認してみます。

echo testと実行するRun Commandを実行しました。Command IDは24f814f5-80aa-429a-ac8b-206b5f249bc3です。

Run Commandの実行結果

SSM AgentのログファイルをこちらのCommand IDでgrepします。

$ sudo grep 24f814f5-80aa-429a-ac8b-206b5f249bc3 /var/log/amazon/ssm/amazon-ssm-agent.log
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Init the cloudwatchlogs publisher
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] document: 24f814f5-80aa-429a-ac8b-206b5f249bc3 worker started
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] channel: 24f814f5-80aa-429a-ac8b-206b5f249bc3 found
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Successfully loaded platform independent plugin aws:runPowerShellScript
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Successfully loaded platform independent plugin aws:updateSsmAgent
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Successfully loaded platform independent plugin aws:configurePackage
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Successfully loaded platform independent plugin aws:downloadContent
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Successfully loaded platform independent plugin aws:runDocument
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Successfully loaded platform independent plugin aws:softwareInventory
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Successfully loaded platform independent plugin aws:configureDocker
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Successfully loaded platform independent plugin aws:runDockerAction
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Successfully loaded platform independent plugin aws:refreshAssociation
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Successfully loaded platform dependent plugin aws:runShellScript
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Successfully loaded platform dependent plugin aws:domainJoin
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] inter process communication started at /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/24f814f5-80aa-429a-ac8b-206b5f249bc3
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] worker listener started on path: /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/24f814f5-80aa-429a-ac8b-206b5f249bc3
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] [DataBackend] received plugin config message
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] [DataBackend] {"DocumentInformation":{"DocumentID":"24f814f5-80aa-429a-ac8b-206b5f249bc3","CommandID":"24f814f5-80aa-429a-ac8b-206b5f249bc3","AssociationID":"","InstanceID":"i-0a2ce926164e897c6","MessageID":"aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6","RunID":"2023-11-03T01-17-46.186Z","CreatedDate":"55808-05-09 00:09:44 +0000 UTC","DocumentName":"AWS-RunShellScript","DocumentVersion":"","DocumentStatus":"InProgress","RunCount":0,"ProcInfo":{"Pid":1499,"StartTime":"2023-11-03T01:17:46.188405338Z"},"ClientId":"","RunAsUser":"","SessionOwner":""},"DocumentType":"SendCommand","SchemaVersion":"1.2","InstancePluginsInformation":[{"Configuration":{"Settings":null,"Properties":[{"id":"0.aws:runShellScript","runCommand":["echo test"],"timeoutSeconds":"3600","workingDirectory":""}],"OutputS3KeyPrefix":"24f814f5-80aa-429a-ac8b-206b5f249bc3/i-0a2ce926164e897c6/awsrunShellScript","OutputS3BucketName":"","S3EncryptionEnabled":false,"CloudWatchLogGroup":"","CloudWatchEncryptionEnabled":false,"CloudWatchStreamingEnabled":false,"OrchestrationDirectory":"/var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/24f814f5-80aa-429a-ac8b-206b5f249bc3/awsrunShellScript","MessageId":"aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6","BookKeepingFileName":"24f814f5-80aa-429a-ac8b-206b5f249bc3","PluginName":"aws:runShellScript","PluginID":"aws:runShellScript","DefaultWorkingDirectory":"","Preconditions":null,"IsPreconditionEnabled":false,"CurrentAssociations":null,"SessionId":"","ClientId":"","KmsKeyId":"","RunAsEnabled":false,"RunAsUser":"","ShellProfile":{"windows":"","linux":""},"SessionOwner":"","UpstreamServiceName":""},"Name":"aws:runShellScript","Result":{"pluginID":"","pluginName":"","status":"","code":0,"output":null,"startDateTime":"0001-01-01T00:00:00Z","endDateTime":"0001-01-01T00:00:00Z","outputS3BucketName":"","outputS3KeyPrefix":"","stepName":"","error":"","standardOutput":"","standardError":""},"Id":"aws:runShellScript"}],"CancelInformation":{"CancelMessageID":"","CancelCommandID":"","Payload":"","DebugInfo":""},"IOConfig":{"OrchestrationDirectory":"/var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/24f814f5-80aa-429a-ac8b-206b5f249bc3","OutputS3BucketName":"","OutputS3KeyPrefix":"24f814f5-80aa-429a-ac8b-206b5f249bc3/i-0a2ce926164e897c6","CloudWatchConfig":{"LogGroupName":"","LogStreamPrefix":"","LogGroupEncryptionEnabled":false}},"UpstreamServiceName":"MessageGatewayService"}
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] [DataBackend] Running plugin aws:runShellScript aws:runShellScript
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] [DataBackend] [pluginName=aws:runShellScript] aws:runShellScript started with configuration {<nil> map[id:0.aws:runShellScript runCommand:[echo test] timeoutSeconds:3600 workingDirectory:] 24f814f5-80aa-429a-ac8b-206b5f249bc3/i-0a2ce926164e897c6/awsrunShellScript  false  false false /var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/24f814f5-80aa-429a-ac8b-206b5f249bc3/awsrunShellScript aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6 24f814f5-80aa-429a-ac8b-206b5f249bc3 aws:runShellScript aws:runShellScript  map[] false []   false  { }  MessageGatewayService}
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] [DataBackend] Sending plugin aws:runShellScript completion message
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] [DataBackend] document execution complete
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] [DataBackend] sending document complete response...
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] [DataBackend] stopping ipc worker...
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] requested shutdown, prepare to stop messaging
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] channel /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/24f814f5-80aa-429a-ac8b-206b5f249bc3 requested close
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Closed the file watcher listener thread
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] worker listener stopped on path: /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/24f814f5-80aa-429a-ac8b-206b5f249bc3
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] channel /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/24f814f5-80aa-429a-ac8b-206b5f249bc3 closed
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] file channel already closed: /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/24f814f5-80aa-429a-ac8b-206b5f249bc3
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] ipc channel closed, stop messaging worker
2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] document worker closed
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] Parsing AgentMessage 26c8f403-1bcd-445e-8253-e990050465f7, Payload: {"schemaVersion":1,"jobId":"aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6","topic":"aws.ssm.sendCommand","content":"{\"OutputS3KeyPrefix\":\"\",\"CloudWatchOutputEnabled\":\"false\",\"Parameters\":{\"executionTimeout\":\"3600\",\"commands\":[\"echo test\"]},\"DocumentContent\":{\"schemaVersion\":\"1.2\",\"description\":\"Run a shell script or specify the commands to run.\",\"runtimeConfig\":{\"aws:runShellScript\":{\"properties\":[{\"workingDirectory\":\"{{ workingDirectory }}\",\"timeoutSeconds\":\"{{ executionTimeout }}\",\"runCommand\":\"{{ commands }}\",\"id\":\"0.aws:runShellScript\"}]}},\"parameters\":{\"executionTimeout\":{\"default\":\"3600\",\"description\":\"(Optional) The time in seconds for a command to complete before it is considered to have failed. Default is 3600 (1 hour). Maximum is 172800 (48 hours).\",\"type\":\"String\",\"allowedPattern\":\"([1-9][0-9]{0,4})|(1[0-6][0-9]{4})|(17[0-1][0-9]{3})|(172[0-7][0-9]{2})|(172800)\"},\"workingDirectory\":{\"default\":\"\",\"description\":\"(Optional) The path to the working directory on your instance.\",\"maxChars\":4096,\"type\":\"String\"},\"commands\":{\"minItems\":1,\"displayType\":\"textarea\",\"description\":\"(Required) Specify a shell script or a command to run.\",\"type\":\"StringList\"}}},\"CloudWatchLogGroupName\":\"\",\"OutputS3Region\":\"us-east-1\",\"CommandId\":\"24f814f5-80aa-429a-ac8b-206b5f249bc3\",\"OutputS3BucketName\":\"\",\"DocumentName\":\"AWS-RunShellScript\"}"}
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] document aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6 submission started
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] document aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6 submission ended
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [CommandProcessorWrapper] [Idempotency] writing command in the idempotency directory for command 24f814f5-80aa-429a-ac8b-206b5f249bc3
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] Got job aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6, starting worker
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] Successfully sent ack message id ff361094-3a69-41bf-8214-b574ec675495 back for JobID aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [24f814f5-80aa-429a-ac8b-206b5f249bc3] channel: 24f814f5-80aa-429a-ac8b-206b5f249bc3 not found, creating a new file channel...
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [24f814f5-80aa-429a-ac8b-206b5f249bc3] master listener started onpath: /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/24f814f5-80aa-429a-ac8b-206b5f249bc3
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [24f814f5-80aa-429a-ac8b-206b5f249bc3] inter process communication started at /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/24f814f5-80aa-429a-ac8b-206b5f249bc3
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [24f814f5-80aa-429a-ac8b-206b5f249bc3] requested terminate messaging worker, destroying the channel
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [24f814f5-80aa-429a-ac8b-206b5f249bc3] channel /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/24f814f5-80aa-429a-ac8b-206b5f249bc3 requested close
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] sending document: 24f814f5-80aa-429a-ac8b-206b5f249bc3 complete response
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [CommandProcessorWrapper] command: aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6complete
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Closed the file watcher listener thread
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [24f814f5-80aa-429a-ac8b-206b5f249bc3] master listener stopped onpath: /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/24f814f5-80aa-429a-ac8b-206b5f249bc3
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] received reply for RunCommandResult aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6 with message id c3432f08-386d-47de-8e90-69237df400e7
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] received reply for RunCommandResult aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6 with message id 19838f76-aa69-4c24-8a1b-f6c357b4156f
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] Got reply msg Id c3432f08-386d-47de-8e90-69237df400e7 for RunCommandResult aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6, starting reply thread
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] Got reply msg Id 19838f76-aa69-4c24-8a1b-f6c357b4156f for RunCommandResult aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6, starting reply thread
2023-11-03 01:17:49 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Stop the cloudwatchlogs publisher
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [24f814f5-80aa-429a-ac8b-206b5f249bc3] channel /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/24f814f5-80aa-429a-ac8b-206b5f249bc3 closed
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [24f814f5-80aa-429a-ac8b-206b5f249bc3] Executer closed
2023-11-03 01:17:46 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] execution of aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6 is over. Removing interimState from current folder

2023-11-03 01:17:46 INFO [ssm-document-worker] [24f814f5-80aa-429a-ac8b-206b5f249bc3] [DataBackend] document execution completeというログからssm-document-workerというプロセスが動作して、実際のコマンドを実行していることが分かります。

そして、よくよく見ると[DataBackend]として以下のログも記録されていました。

{
  "DocumentInformation": {
    "DocumentID": "24f814f5-80aa-429a-ac8b-206b5f249bc3",
    "CommandID": "24f814f5-80aa-429a-ac8b-206b5f249bc3",
    "AssociationID": "",
    "InstanceID": "i-0a2ce926164e897c6",
    "MessageID": "aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6",
    "RunID": "2023-11-03T01-17-46.186Z",
    "CreatedDate": "55808-05-09 00:09:44 +0000 UTC",
    "DocumentName": "AWS-RunShellScript",
    "DocumentVersion": "",
    "DocumentStatus": "InProgress",
    "RunCount": 0,
    "ProcInfo": {
      "Pid": 1499,
      "StartTime": "2023-11-03T01:17:46.188405338Z"
    },
    "ClientId": "",
    "RunAsUser": "",
    "SessionOwner": ""
  },
  "DocumentType": "SendCommand",
  "SchemaVersion": "1.2",
  "InstancePluginsInformation": [
    {
      "Configuration": {
        "Settings": null,
        "Properties": [
          {
            "id": "0.aws:runShellScript",
            "runCommand": [
              "echo test"
            ],
            "timeoutSeconds": "3600",
            "workingDirectory": ""
          }
        ],
        "OutputS3KeyPrefix": "24f814f5-80aa-429a-ac8b-206b5f249bc3/i-0a2ce926164e897c6/awsrunShellScript",
        "OutputS3BucketName": "",
        "S3EncryptionEnabled": false,
        "CloudWatchLogGroup": "",
        "CloudWatchEncryptionEnabled": false,
        "CloudWatchStreamingEnabled": false,
        "OrchestrationDirectory": "/var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/24f814f5-80aa-429a-ac8b-206b5f249bc3/awsrunShellScript",
        "MessageId": "aws.ssm.24f814f5-80aa-429a-ac8b-206b5f249bc3.i-0a2ce926164e897c6",
        "BookKeepingFileName": "24f814f5-80aa-429a-ac8b-206b5f249bc3",
        "PluginName": "aws:runShellScript",
        "PluginID": "aws:runShellScript",
        "DefaultWorkingDirectory": "",
        "Preconditions": null,
        "IsPreconditionEnabled": false,
        "CurrentAssociations": null,
        "SessionId": "",
        "ClientId": "",
        "KmsKeyId": "",
        "RunAsEnabled": false,
        "RunAsUser": "",
        "ShellProfile": {
          "windows": "",
          "linux": ""
        },
        "SessionOwner": "",
        "UpstreamServiceName": ""
      },
      "Name": "aws:runShellScript",
      "Result": {
        "pluginID": "",
        "pluginName": "",
        "status": "",
        "code": 0,
        "output": null,
        "startDateTime": "0001-01-01T00:00:00Z",
        "endDateTime": "0001-01-01T00:00:00Z",
        "outputS3BucketName": "",
        "outputS3KeyPrefix": "",
        "stepName": "",
        "error": "",
        "standardOutput": "",
        "standardError": ""
      },
      "Id": "aws:runShellScript"
    }
  ],
  "CancelInformation": {
    "CancelMessageID": "",
    "CancelCommandID": "",
    "Payload": "",
    "DebugInfo": ""
  },
  "IOConfig": {
    "OrchestrationDirectory": "/var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/24f814f5-80aa-429a-ac8b-206b5f249bc3",
    "OutputS3BucketName": "",
    "OutputS3KeyPrefix": "24f814f5-80aa-429a-ac8b-206b5f249bc3/i-0a2ce926164e897c6",
    "CloudWatchConfig": {
      "LogGroupName": "",
      "LogStreamPrefix": "",
      "LogGroupEncryptionEnabled": false
    }
  },
  "UpstreamServiceName": "MessageGatewayService"
}

Command IDはもちろん、渡されたパラメーターなども記録されていますね。

ここで注目すべきはDocumentInformation.ProcInfo.Pidです。

以下記事でSSM Session ManagerのプロセスはSSM Agentの子プロセスであることを確認しました。

Run Commandで渡されたコマンドはssm-document-workerで実行されていそうであることを確認しました。つまり、渡されたコマンド上で親プロセスのID(PPID)を取得すれば、上述のログからCommand IDを取得することができるのではないでしょうか。

試しに以下コマンドをRun Commandで渡してみます。

#!/bin/bash

# SSM Agentのステータスの確認
systemctl status amazon-ssm-agent.service

# プロセスツリーの確認
pstree -ulpc

実行結果は以下のとおりです。

● amazon-ssm-agent.service - amazon-ssm-agent
     Loaded: loaded (/etc/systemd/system/amazon-ssm-agent.service; enabled; preset: disabled)
     Active: active (running) since Fri 2023-11-03 01:15:48 UTC; 38min ago
   Main PID: 951 (amazon-ssm-agen)
      Tasks: 42 (limit: 4260)
     Memory: 236.6M
        CPU: 8.873s
     CGroup: /system.slice/amazon-ssm-agent.service
             ├─ 951 /usr/bin/amazon-ssm-agent
             ├─1329 /usr/bin/ssm-agent-worker
             ├─1519 /usr/bin/ssm-session-worker <IAMユーザー名>-08b650a06d34643c8
             ├─1531 sh
             ├─1532 /bin/bash
             ├─1884 /usr/bin/ssm-document-worker a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae
             ├─1890 /bin/bash /var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae/awsrunShellScript/0.awsrunShellScript/_script.sh
             └─1891 systemctl status amazon-ssm-agent.service

Nov 03 01:15:49 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] [WorkerProvider] Worker ssm-agent-worker is not running, starting worker process
Nov 03 01:15:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] [WorkerProvider] Worker ssm-agent-worker (pid:1329) started
Nov 03 01:15:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] Monitor long running worker health every 60 seconds
Nov 03 01:21:42 ip-172-31-86-227.ec2.internal sudo[1553]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/su -ls -l /var/log/amazon/ssm/
Nov 03 01:21:48 ip-172-31-86-227.ec2.internal sudo[1567]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/ls -l /var/log/amazon/ssm/
Nov 03 01:22:00 ip-172-31-86-227.ec2.internal sudo[1574]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/grep 24f814f5-80aa-429a-ac8b-206b5f249bc3 /var/log/amazon/ssm/amazon-ssm-agent.log
Nov 03 01:45:49 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:45:49 INFO EC2RoleProvider Successfully connected with instance profile role credentials
Nov 03 01:45:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:45:49 INFO [CredentialRefresher] Credentials ready
Nov 03 01:45:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:45:49 INFO [CredentialRefresher] Next credential rotation will be in 29.999988924266667 minutes
Nov 03 01:46:03 ip-172-31-86-227.ec2.internal sudo[1724]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/ps --antp
systemd(1)-+-NetworkManager(640)-+-{NetworkManager}(641)
           |                     `-{NetworkManager}(642)
           |-agetty(962)
           |-agetty(965)
           |-amazon-ssm-agen(951)-+-ssm-agent-worke(1329)-+-ssm-document-wo(1884)-+-_script.sh(1890)---pstree(1892)
           |                      |                       |                       |-{ssm-document-wo}(1885)
           |                      |                       |                       |-{ssm-document-wo}(1886)
           |                      |                       |                       |-{ssm-document-wo}(1887)
           |                      |                       |                       |-{ssm-document-wo}(1888)
           |                      |                       |                       |-{ssm-document-wo}(1889)
           |                      |                       |                       `-{ssm-document-wo}(1893)
           |                      |                       |-ssm-session-wor(1519)-+-sh(1531,ec2-user)---bash(1532)
           |                      |                       |                       |-{ssm-session-wor}(1520)
           |                      |                       |                       |-{ssm-session-wor}(1521)
           |                      |                       |                       |-{ssm-session-wor}(1522)
           |                      |                       |                       |-{ssm-session-wor}(1523)
           |                      |                       |                       |-{ssm-session-wor}(1524)
           |                      |                       |                       |-{ssm-session-wor}(1525)
           |                      |                       |                       |-{ssm-session-wor}(1526)
           |                      |                       |                       `-{ssm-session-wor}(1551)
           |                      |                       |-{ssm-agent-worke}(1332)
           |                      |                       |-{ssm-agent-worke}(1333)
           |                      |                       |-{ssm-agent-worke}(1334)
           |                      |                       |-{ssm-agent-worke}(1335)
           |                      |                       |-{ssm-agent-worke}(1337)
           |                      |                       |-{ssm-agent-worke}(1338)
           |                      |                       |-{ssm-agent-worke}(1339)
           |                      |                       |-{ssm-agent-worke}(1340)
           |                      |                       |-{ssm-agent-worke}(1456)
           |                      |                       |-{ssm-agent-worke}(1459)
           |                      |                       |-{ssm-agent-worke}(1460)
           |                      |                       |-{ssm-agent-worke}(1478)
           |                      |                       |-{ssm-agent-worke}(1480)
           |                      |                       `-{ssm-agent-worke}(1481)
           |                      |-{amazon-ssm-agen}(957)
           |                      |-{amazon-ssm-agen}(958)
           |                      |-{amazon-ssm-agen}(959)
           |                      |-{amazon-ssm-agen}(960)
           |                      |-{amazon-ssm-agen}(963)
           |                      |-{amazon-ssm-agen}(964)
           |                      `-{amazon-ssm-agen}(1328)
           |-auditd(574)---{auditd}(575)
           |-chronyd(616,chrony)
           |-crond(961)
           |-dbus-broker-lau(599,dbus)---dbus-broker(605)
           |-gssproxy(654)-+-{gssproxy}(656)
           |               |-{gssproxy}(657)
           |               |-{gssproxy}(658)
           |               |-{gssproxy}(659)
           |               `-{gssproxy}(660)
           |-irqbalance(608)---{irqbalance}(617)
           |-polkitd(902,polkitd)-+-{polkitd}(912)
           |                      |-{polkitd}(913)
           |                      |-{polkitd}(915)
           |                      |-{polkitd}(916)
           |                      `-{polkitd}(919)
           |-rhsmcertd(652)---{rhsmcertd}(653)
           |-rpcbind(573,rpc)
           |-rsyslogd(609)-+-{rsyslogd}(613)
           |               `-{rsyslogd}(614)
           |-sshd(955)
           |-systemd-journal(499)
           |-systemd-logind(610)
           |-systemd-udevd(512)
           `-tuned(651)-+-{tuned}(903)
                        |-{tuned}(906)
                        `-{tuned}(909)

Run Commandで実行されるコマンドは_script.shの子プロセスで、_script.shの親プロセスはssm-document-workerであることが分かります。ssm-document-workerのPIDは1884です。

Command IDa042e1f0-5fa6-4bf1-bf6c-1c64cf79c2aeのSSM Agentのログを確認して、PIDが1884か確認します。

$ sudo grep a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae /var/log/amazon/ssm/amazon-ssm-agent.log
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Init the cloudwatchlogs publisher
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] document: a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae worker started
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] channel: a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae found
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Successfully loaded platform independent plugin aws:downloadContent
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Successfully loaded platform independent plugin aws:runDocument
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Successfully loaded platform independent plugin aws:softwareInventory
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Successfully loaded platform independent plugin aws:runPowerShellScript
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Successfully loaded platform independent plugin aws:updateSsmAgent
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Successfully loaded platform independent plugin aws:configureDocker
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Successfully loaded platform independent plugin aws:configurePackage
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Successfully loaded platform independent plugin aws:runDockerAction
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Successfully loaded platform independent plugin aws:refreshAssociation
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Successfully loaded platform dependent plugin aws:runShellScript
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Successfully loaded platform dependent plugin aws:domainJoin
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] inter process communication started at /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] worker listener started on path: /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] [DataBackend] received plugin config message
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] [DataBackend] {"DocumentInformation":{"DocumentID":"a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae","CommandID":"a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae","AssociationID":"","InstanceID":"i-0a2ce926164e897c6","MessageID":"aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6","RunID":"2023-11-03T01-54-48.572Z","CreatedDate":"55808-06-03 17:29:29 +0000 UTC","DocumentName":"AWS-RunShellScript","DocumentVersion":"","DocumentStatus":"InProgress","RunCount":0,"ProcInfo":{"Pid":1884,"StartTime":"2023-11-03T01:54:48.573653529Z"},"ClientId":"","RunAsUser":"","SessionOwner":""},"DocumentType":"SendCommand","SchemaVersion":"1.2","InstancePluginsInformation":[{"Configuration":{"Settings":null,"Properties":[{"id":"0.aws:runShellScript","runCommand":["#!/bin/bash","","# SSM Agentのステータスの確認","systemctl status amazon-ssm-agent.service","","# プロセスツリーの確認","pstree -ulpc"],"timeoutSeconds":"3600","workingDirectory":""}],"OutputS3KeyPrefix":"a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae/i-0a2ce926164e897c6/awsrunShellScript","OutputS3BucketName":"","S3EncryptionEnabled":false,"CloudWatchLogGroup":"","CloudWatchEncryptionEnabled":false,"CloudWatchStreamingEnabled":false,"OrchestrationDirectory":"/var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae/awsrunShellScript","MessageId":"aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6","BookKeepingFileName":"a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae","PluginName":"aws:runShellScript","PluginID":"aws:runShellScript","DefaultWorkingDirectory":"","Preconditions":null,"IsPreconditionEnabled":false,"CurrentAssociations":null,"SessionId":"","ClientId":"","KmsKeyId":"","RunAsEnabled":false,"RunAsUser":"","ShellProfile":{"windows":"","linux":""},"SessionOwner":"","UpstreamServiceName":""},"Name":"aws:runShellScript","Result":{"pluginID":"","pluginName":"","status":"","code":0,"output":null,"startDateTime":"0001-01-01T00:00:00Z","endDateTime":"0001-01-01T00:00:00Z","outputS3BucketName":"","outputS3KeyPrefix":"","stepName":"","error":"","standardOutput":"","standardError":""},"Id":"aws:runShellScript"}],"CancelInformation":{"CancelMessageID":"","CancelCommandID":"","Payload":"","DebugInfo":""},"IOConfig":{"OrchestrationDirectory":"/var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae","OutputS3BucketName":"","OutputS3KeyPrefix":"a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae/i-0a2ce926164e897c6","CloudWatchConfig":{"LogGroupName":"","LogStreamPrefix":"","LogGroupEncryptionEnabled":false}},"UpstreamServiceName":"MessageGatewayService"}
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] [DataBackend] Running plugin aws:runShellScript aws:runShellScript
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] [DataBackend] [pluginName=aws:runShellScript] aws:runShellScript started with configuration {<nil> map[id:0.aws:runShellScript runCommand:[#!/bin/bash  # SSM Agentのステータスの確認 systemctl status amazon-ssm-agent.service  # プロセスツリーの確認 pstree -ulpc] timeoutSeconds:3600 workingDirectory:] a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae/i-0a2ce926164e897c6/awsrunShellScript  false  false false/var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae/awsrunShellScript aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6 a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae aws:runShellScript aws:runShellScript  map[] false []    false  { }  MessageGatewayService}
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] [DataBackend] Sending plugin aws:runShellScript completion message
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] [DataBackend] document execution complete
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] [DataBackend] sending document complete response...
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] [DataBackend] stopping ipc worker...
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] requested shutdown, prepare to stop messaging
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] channel /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae requested close
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Closed the file watcher listener thread
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] worker listener stopped on path: /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] channel /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae closed
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] file channel already closed: /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] ipc channel closed, stop messaging worker
2023-11-03 01:54:48 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] document worker closed
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] Parsing AgentMessage 52cebd25-3fcb-4291-9a8f-0b2444f6fdb7, Payload: {"schemaVersion":1,"jobId":"aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6","topic":"aws.ssm.sendCommand","content":"{\"OutputS3KeyPrefix\":\"\",\"CloudWatchOutputEnabled\":\"false\",\"Parameters\":{\"executionTimeout\":\"3600\",\"commands\":[\"#!/bin/bash\",\"\",\"# SSM Agentのステータスの確認\",\"systemctl status amazon-ssm-agent.service\",\"\",\"# プロセスツリーの確認\",\"pstree -ulpc\"]},\"DocumentContent\":{\"schemaVersion\":\"1.2\",\"description\":\"Run a shell script or specify the commands to run.\",\"runtimeConfig\":{\"aws:runShellScript\":{\"properties\":[{\"workingDirectory\":\"{{ workingDirectory }}\",\"timeoutSeconds\":\"{{ executionTimeout }}\",\"runCommand\":\"{{ commands }}\",\"id\":\"0.aws:runShellScript\"}]}},\"parameters\":{\"executionTimeout\":{\"default\":\"3600\",\"description\":\"(Optional) The time in seconds for a command to complete before it is considered to have failed. Default is 3600 (1 hour). Maximum is 172800 (48 hours).\",\"type\":\"String\",\"allowedPattern\":\"([1-9][0-9]{0,4})|(1[0-6][0-9]{4})|(17[0-1][0-9]{3})|(172[0-7][0-9]{2})|(172800)\"},\"workingDirectory\":{\"default\":\"\",\"description\":\"(Optional) The path to the working directory on your instance.\",\"maxChars\":4096,\"type\":\"String\"},\"commands\":{\"minItems\":1,\"displayType\":\"textarea\",\"description\":\"(Required) Specify a shell script or a command to run.\",\"type\":\"StringList\"}}},\"CloudWatchLogGroupName\":\"\",\"OutputS3Region\":\"us-east-1\",\"CommandId\":\"a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae\",\"OutputS3BucketName\":\"\",\"DocumentName\":\"AWS-RunShellScript\"}"}
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] document aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6 submission started
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] document aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6 submission ended
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [CommandProcessorWrapper] [Idempotency] writing command in the idempotency directory for command a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] Got job aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6, starting worker
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] Successfully sent ack message id 1516f4bb-16c1-4b16-b89f-babb00a8c33a back for JobID aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] channel: a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae not found, creating a new file channel...
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] master listener started onpath: /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] inter process communication started at /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] received reply for RunCommandResult aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6 with message id 400be319-dd2d-4bc6-b6d1-4891addd8867
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] requested terminate messaging worker, destroying the channel
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] channel /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae requested close
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] sending document: a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae complete response
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [CommandProcessorWrapper] command: aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6complete
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Closed the file watcher listener thread
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] master listener stopped onpath: /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] channel /var/lib/amazon/ssm/i-0a2ce926164e897c6/channels/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae closed
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] received reply for RunCommandResult aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6 with message id 0b9b480a-852a-417e-b2c3-0126e5db2e4e
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] Got reply msg Id 400be319-dd2d-4bc6-b6d1-4891addd8867 for RunCommandResult aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6, starting reply thread
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] Got reply msg Id 0b9b480a-852a-417e-b2c3-0126e5db2e4e for RunCommandResult aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6, starting reply thread
2023-11-03 01:54:51 INFO [ssm-document-worker] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Stop the cloudwatchlogs publisher
      "output": "● amazon-ssm-agent.service - amazon-ssm-agent\n     Loaded: loaded (/etc/systemd/system/amazon-ssm-agent.service; enabled; preset: disabled)\n   Active: active (running) since Fri 2023-11-03 01:15:48 UTC; 38min ago\n   Main PID: 951 (amazon-ssm-agen)\n      Tasks: 42 (limit: 4260)\n     Memory: 236.6M\n        CPU: 8.873s\n     CGroup: /system.slice/amazon-ssm-agent.service\n             ├─ 951 /usr/bin/amazon-ssm-agent\n             ├─1329 /usr/bin/ssm-agent-worker\n             ├─1519 /usr/bin/ssm-session-worker <IAMユーザー名>-08b650a06d34643c8\n             ├─1531 sh\n             ├─1532 /bin/bash\n├─1884 /usr/bin/ssm-document-worker a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae\n             ├─1890 /bin/bash /var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae/awsrunShellScript/0.awsrunShellScript/_script.sh\n             └─1891 systemctl status amazon-ssm-agent.service\n\nNov 03 01:15:49 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] [WorkerProvider] Worker ssm-agent-worker is not running, starting worker process\nNov 03 01:15:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] [WorkerProvider] Worker ssm-agent-worker (pid:1329) started\nNov 03 01:15:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] Monitor long running worker health every 60 seconds\nNov 03 01:21:42 ip-172-31-86-227.ec2.internal sudo[1553]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/su -ls -l /var/log/amazon/ssm/\nNov 03 01:21:48 ip-172-31-86-227.ec2.internal sudo[1567]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/ls -l /var/log/amazon/ssm/\nNov 03 01:22:00 ip-172-31-86-227.ec2.internal sudo[1574]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/grep 24f814f5-80aa-429a-ac8b-206b5f249bc3 /var/log/amazon/ssm/amazon-ssm-agent.log\nNov 03 01:45:49 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:45:49 INFO EC2RoleProvider Successfully connected with instance profile role credentials\nNov 03 01:45:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:45:49 INFO [CredentialRefresher] Credentials ready\nNov 03 01:45:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]\n---Output truncated---",
      "standardOutput": "● amazon-ssm-agent.service - amazon-ssm-agent\n     Loaded: loaded (/etc/systemd/system/amazon-ssm-agent.service; enabled; preset: disabled)\n     Active: active (running) since Fri 2023-11-03 01:15:48 UTC; 38min ago\n   Main PID: 951 (amazon-ssm-agen)\n      Tasks: 42 (limit: 4260)\n     Memory: 236.6M\n        CPU: 8.873s\n     CGroup: /system.slice/amazon-ssm-agent.service\n             ├─ 951 /usr/bin/amazon-ssm-agent\n             ├─1329 /usr/bin/ssm-agent-worker\n             ├─1519 /usr/bin/ssm-session-worker <IAMユーザー名>-08b650a06d34643c8\n             ├─1531 sh\n             ├─1532 /bin/bash\n        ├─1884 /usr/bin/ssm-document-worker a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae\n             ├─1890 /bin/bash /var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae/awsrunShellScript/0.awsrunShellScript/_script.sh\n             └─1891 systemctl status amazon-ssm-agent.service\n\nNov 03 01:15:49 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] [WorkerProvider] Worker ssm-agent-worker is not running, starting worker process\nNov 03 01:15:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] [WorkerProvider] Worker ssm-agent-worker (pid:1329) started\nNov 03 01:15:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] Monitor long running worker health every 60 seconds\nNov 03 01:21:42 ip-172-31-86-227.ec2.internal sudo[1553]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/su -ls -l /var/log/amazon/ssm/\nNov 03 01:21:48 ip-172-31-86-227.ec2.internal sudo[1567]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/ls -l /var/log/amazon/ssm/\nNov 03 01:22:00 ip-172-31-86-227.ec2.internal sudo[1574]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/grep 24f814f5-80aa-429a-ac8b-206b5f249bc3 /var/log/amazon/ssm/amazon-ssm-agent.log\nNov 03 01:45:49 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:45:49 INFO EC2RoleProvider Successfully connected with instance profile role credentials\nNov 03 01:45:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:45:49 INFO [CredentialRefresher] Credentials ready\nNov 03 01:45:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:45:49 INFO [CredentialRefresher] Next credential rotation will be in 29.999988924266667 minutes\nNov 03 01:46:03 ip-172-31-86-227.ec2.internal sudo[1724]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/ps --antp\nsystemd(1)-+-NetworkManager(640)-+-{NetworkManager}(641)\n           |                     `-{NetworkManager}(642)\n           |-agetty(962)\n           |-agetty(965)\n           |-amazon-ssm-agen(951)-+-ssm-agent-worke(1329)-+-ssm-document-wo(1884)-+-_script.sh(1890)---pstree(1892)\n           |    |                       |                       |-{ssm-document-wo}(1885)\n           |                      |                       ||-{ssm-document-wo}(1886)\n           |                      |                       |                       |-{ssm-document-wo}(1887)\n           |         |                       |                       |-{ssm-document-wo}(1888)\n           |                      |                       |     |-{ssm-document-wo}(1889)\n           |                      |                       |                       `-{ssm-document-wo}(1893)\n           |              |                       |-ssm-session-wor(1519)-+-sh(1531,ec2-user)---bash(1532)\n           |                      |                       |                 |-{ssm-session-wor}(1520)\n           |                      |                       |                       |-{ssm-session-wor}(1521)\n   |                      |                       |                       |-{ssm-session-wor}(1522)\n           |                      |                       |                      |-{ssm-session-wor}(1523)\n           |                      |                       |                       |-{ssm-session-wor}(1524)\n        |                      |                       |                       |-{ssm-session-wor}(1525)\n           |                      |   |                       |-{ssm-session-wor}(1526)\n           |                      |                       |                       `-{ssm-session-wor}(1551)\n           |                      |                       |-{ssm-agent-worke}(1332)\n           |                      |                       |-{ssm-agent-worke}(1333)\n           |                      |                       |-{ssm-agent-worke}(1334)\n           |                      |                       |-{ssm-agent-worke}(1335)\n           |                      |                       |-{ssm-agent-worke}(1337)\n           |                      |  |-{ssm-agent-worke}(1338)\n           |                      |                       |-{ssm-agent-worke}(1339)\n           |                      |           |-{ssm-agent-worke}(1340)\n           |                      |                       |-{ssm-agent-worke}(1456)\n           |                      |                    |-{ssm-agent-worke}(1459)\n           |                      |                       |-{ssm-agent-worke}(1460)\n           |     |                       |-{ssm-agent-worke}(1478)\n           |                      |                       |-{ssm-agent-worke}(1480)\n           |              |                       `-{ssm-agent-worke}(1481)\n           |                      |-{amazon-ssm-agen}(957)\n           |                      |-{amazon-ssm-agen}(958)\n           |                      |-{amazon-ssm-agen}(959)\n           |                      |-{amazon-ssm-agen}(960)\n           |                 |-{amazon-ssm-agen}(963)\n           |                      |-{amazon-ssm-agen}(964)\n           |                      `-{amazon-ssm-agen}(1328)\n           |-auditd(574)---{auditd}(575)\n           |-chronyd(616,chrony)\n           |-crond(961)\n           |-dbus-broker-lau(599,dbus)---dbus-broker(605)\n           |-gssproxy(654)-+-{gssproxy}(656)\n           |               |-{gssproxy}(657)\n           |               |-{gssproxy}(658)\n           |      |-{gssproxy}(659)\n           |               `-{gssproxy}(660)\n           |-irqbalance(608)---{irqbalance}(617)\n           |-polkitd(902,polkitd)-+-{polkitd}(912)\n           |                      |-{polkitd}(913)\n           |                      |-{polkitd}(915)\n           |                      |-{polkitd}(916)\n           |                      `-{polkitd}(919)\n           |-rhsmcertd(652)---{rhsmcertd}(653)\n           |-rpcbind(573,rpc)\n           |-rsyslogd(609)-+-{rsyslogd}(613)\n           |               `-{rsyslogd}(614)\n           |-sshd(955)\n           |-systemd-journal(499)\n           |-systemd-logind(610)\n           |-systemd-udevd(512)\n           `-tuned(651)-+-{tuned}(903)\n                        |-{tuned}(906)\n                        `-{tuned}(909)\n",
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] [BasicExecuter] [a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae] Executer closed
2023-11-03 01:54:48 INFO [ssm-agent-worker] [MessageService] [EngineProcessor] execution of aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6 is over. Removing interimState from current folder
      "output": "● amazon-ssm-agent.service - amazon-ssm-agent\n     Loaded: loaded (/etc/systemd/system/amazon-ssm-agent.service; enabled; preset: disabled)\n   Active: active (running) since Fri 2023-11-03 01:15:48 UTC; 38min ago\n   Main PID: 951 (amazon-ssm-agen)\n      Tasks: 42 (limit: 4260)\n     Memory: 236.6M\n        CPU: 8.873s\n     CGroup: /system.slice/amazon-ssm-agent.service\n             ├─ 951 /usr/bin/amazon-ssm-agent\n             ├─1329 /usr/bin/ssm-agent-worker\n             ├─1519 /usr/bin/ssm-session-worker <IAMユーザー名>-08b650a06d34643c8\n             ├─1531 sh\n             ├─1532 /bin/bash\n├─1884 /usr/bin/ssm-document-worker a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae\n             ├─1890 /bin/bash /var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae/awsrunShellScript/0.awsrunShellScript/_script.sh\n             └─1891 systemctl status amazon-ssm-agent.service\n\nNov 03 01:15:49 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] [WorkerProvider] Worker ssm-agent-worker is not running, starting worker process\nNov 03 01:15:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] [WorkerProvider] Worker ssm-agent-worker (pid:1329) started\nNov 03 01:15:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] Monitor long running worker health every 60 seconds\nNov 03 01:21:42 ip-172-31-86-227.ec2.internal sudo[1553]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/su -ls -l /var/log/amazon/ssm/\nNov 03 01:21:48 ip-172-31-86-227.ec2.internal sudo[1567]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/ls -l /var/log/amazon/ssm/\nNov 03 01:22:00 ip-172-31-86-227.ec2.internal sudo[1574]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/grep 24f814f5-80aa-429a-ac8b-206b5f249bc3 /var/log/amazon/ssm/amazon-ssm-agent.log\nNov 03 01:45:49 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:45:49 INFO EC2RoleProvider Successfully connected with instance profile role credentials\nNov 03 01:45:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:45:49 INFO [CredentialRefresher] Credentials ready\nNov 03 01:45:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]\n---Output truncated---",
      "standardOutput": "● amazon-ssm-agent.service - amazon-ssm-agent\n     Loaded: loaded (/etc/systemd/system/amazon-ssm-agent.service; enabled; preset: disabled)\n     Active: active (running) since Fri 2023-11-03 01:15:48 UTC; 38min ago\n   Main PID: 951 (amazon-ssm-agen)\n      Tasks: 42 (limit: 4260)\n     Memory: 236.6M\n        CPU: 8.873s\n     CGroup: /system.slice/amazon-ssm-agent.service\n             ├─ 951 /usr/bin/amazon-ssm-agent\n             ├─1329 /usr/bin/ssm-agent-worker\n             ├─1519 /usr/bin/ssm-session-worker <IAMユーザー名>-08b650a06d34643c8\n             ├─1531 sh\n             ├─1532 /bin/bash\n        ├─1884 /usr/bin/ssm-document-worker a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae\n             ├─1890 /bin/bash /var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae/awsrunShellScript/0.awsrunShellScript/_script.sh\n             └─1891 systemctl status amazon-ssm-agent.service\n\nNov 03 01:15:49 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] [WorkerProvider] Worker ssm-agent-worker is not running, starting worker process\nNov 03 01:15:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] [WorkerProvider] Worker ssm-agent-worker (pid:1329) started\nNov 03 01:15:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:15:49 INFO [amazon-ssm-agent] [LongRunningWorkerContainer] Monitor long running worker health every 60 seconds\nNov 03 01:21:42 ip-172-31-86-227.ec2.internal sudo[1553]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/su -ls -l /var/log/amazon/ssm/\nNov 03 01:21:48 ip-172-31-86-227.ec2.internal sudo[1567]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/ls -l /var/log/amazon/ssm/\nNov 03 01:22:00 ip-172-31-86-227.ec2.internal sudo[1574]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/grep 24f814f5-80aa-429a-ac8b-206b5f249bc3 /var/log/amazon/ssm/amazon-ssm-agent.log\nNov 03 01:45:49 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:45:49 INFO EC2RoleProvider Successfully connected with instance profile role credentials\nNov 03 01:45:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:45:49 INFO [CredentialRefresher] Credentials ready\nNov 03 01:45:50 ip-172-31-86-227.ec2.internal amazon-ssm-agent[951]: 2023-11-03 01:45:49 INFO [CredentialRefresher] Next credential rotation will be in 29.999988924266667 minutes\nNov 03 01:46:03 ip-172-31-86-227.ec2.internal sudo[1724]: ec2-user : TTY=pts/0 ; PWD=/home/ec2-user ; USER=root ; COMMAND=/bin/ps --antp\nsystemd(1)-+-NetworkManager(640)-+-{NetworkManager}(641)\n           |                     `-{NetworkManager}(642)\n           |-agetty(962)\n           |-agetty(965)\n           |-amazon-ssm-agen(951)-+-ssm-agent-worke(1329)-+-ssm-document-wo(1884)-+-_script.sh(1890)---pstree(1892)\n           |    |                       |                       |-{ssm-document-wo}(1885)\n           |                      |                       ||-{ssm-document-wo}(1886)\n           |                      |                       |                       |-{ssm-document-wo}(1887)\n           |         |                       |                       |-{ssm-document-wo}(1888)\n           |                      |                       |     |-{ssm-document-wo}(1889)\n           |                      |                       |                       `-{ssm-document-wo}(1893)\n           |              |                       |-ssm-session-wor(1519)-+-sh(1531,ec2-user)---bash(1532)\n           |                      |                       |                 |-{ssm-session-wor}(1520)\n           |                      |                       |                       |-{ssm-session-wor}(1521)\n   |                      |                       |                       |-{ssm-session-wor}(1522)\n           |                      |                       |                      |-{ssm-session-wor}(1523)\n           |                      |                       |                       |-{ssm-session-wor}(1524)\n        |                      |                       |                       |-{ssm-session-wor}(1525)\n           |                      |   |                       |-{ssm-session-wor}(1526)\n           |                      |                       |                       `-{ssm-session-wor}(1551)\n           |                      |                       |-{ssm-agent-worke}(1332)\n           |                      |                       |-{ssm-agent-worke}(1333)\n           |                      |                       |-{ssm-agent-worke}(1334)\n           |                      |                       |-{ssm-agent-worke}(1335)\n           |                      |                       |-{ssm-agent-worke}(1337)\n           |                      |  |-{ssm-agent-worke}(1338)\n           |                      |                       |-{ssm-agent-worke}(1339)\n           |                      |           |-{ssm-agent-worke}(1340)\n           |                      |                       |-{ssm-agent-worke}(1456)\n           |                      |                    |-{ssm-agent-worke}(1459)\n           |                      |                       |-{ssm-agent-worke}(1460)\n           |     |                       |-{ssm-agent-worke}(1478)\n           |                      |                       |-{ssm-agent-worke}(1480)\n           |              |                       `-{ssm-agent-worke}(1481)\n           |                      |-{amazon-ssm-agen}(957)\n           |                      |-{amazon-ssm-agen}(958)\n           |                      |-{amazon-ssm-agen}(959)\n           |                      |-{amazon-ssm-agen}(960)\n           |                 |-{amazon-ssm-agen}(963)\n           |                      |-{amazon-ssm-agen}(964)\n           |                      `-{amazon-ssm-agen}(1328)\n           |-auditd(574)---{auditd}(575)\n           |-chronyd(616,chrony)\n           |-crond(961)\n           |-dbus-broker-lau(599,dbus)---dbus-broker(605)\n           |-gssproxy(654)-+-{gssproxy}(656)\n           |               |-{gssproxy}(657)\n           |               |-{gssproxy}(658)\n           |      |-{gssproxy}(659)\n           |               `-{gssproxy}(660)\n           |-irqbalance(608)---{irqbalance}(617)\n           |-polkitd(902,polkitd)-+-{polkitd}(912)\n           |                      |-{polkitd}(913)\n           |                      |-{polkitd}(915)\n           |                      |-{polkitd}(916)\n           |                      `-{polkitd}(919)\n           |-rhsmcertd(652)---{rhsmcertd}(653)\n           |-rpcbind(573,rpc)\n           |-rsyslogd(609)-+-{rsyslogd}(613)\n           |               `-{rsyslogd}(614)\n           |-sshd(955)\n           |-systemd-journal(499)\n           |-systemd-logind(610)\n           |-systemd-udevd(512)\n           `-tuned(651)-+-{tuned}(903)\n                        |-{tuned}(906)\n                        `-{tuned}(909)\n",

以下のように確かにDocumentInformation.ProcInfo.Pid1884でした。

{
  "DocumentInformation": {
    "DocumentID": "a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae",
    "CommandID": "a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae",
    "AssociationID": "",
    "InstanceID": "i-0a2ce926164e897c6",
    "MessageID": "aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6",
    "RunID": "2023-11-03T01-54-48.572Z",
    "CreatedDate": "55808-06-03 17:29:29 +0000 UTC",
    "DocumentName": "AWS-RunShellScript",
    "DocumentVersion": "",
    "DocumentStatus": "InProgress",
    "RunCount": 0,
    "ProcInfo": {
      "Pid": 1884,
      "StartTime": "2023-11-03T01:54:48.573653529Z"
    },
    "ClientId": "",
    "RunAsUser": "",
    "SessionOwner": ""
  },
  "DocumentType": "SendCommand",
  "SchemaVersion": "1.2",
  "InstancePluginsInformation": [
    {
      "Configuration": {
        "Settings": null,
        "Properties": [
          {
            "id": "0.aws:runShellScript",
            "runCommand": [
              "#!/bin/bash",
              "",
              "# SSM Agentのステータスの確認",
              "systemctl status amazon-ssm-agent.service",
              "",
              "# プロセスツリーの確認",
              "pstree -ulpc"
            ],
            "timeoutSeconds": "3600",
            "workingDirectory": ""
          }
        ],
        "OutputS3KeyPrefix": "a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae/i-0a2ce926164e897c6/awsrunShellScript",
        "OutputS3BucketName": "",
        "S3EncryptionEnabled": false,
        "CloudWatchLogGroup": "",
        "CloudWatchEncryptionEnabled": false,
        "CloudWatchStreamingEnabled": false,
        "OrchestrationDirectory": "/var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae/awsrunShellScript",
        "MessageId": "aws.ssm.a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae.i-0a2ce926164e897c6",
        "BookKeepingFileName": "a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae",
        "PluginName": "aws:runShellScript",
        "PluginID": "aws:runShellScript",
        "DefaultWorkingDirectory": "",
        "Preconditions": null,
        "IsPreconditionEnabled": false,
        "CurrentAssociations": null,
        "SessionId": "",
        "ClientId": "",
        "KmsKeyId": "",
        "RunAsEnabled": false,
        "RunAsUser": "",
        "ShellProfile": {
          "windows": "",
          "linux": ""
        },
        "SessionOwner": "",
        "UpstreamServiceName": ""
      },
      "Name": "aws:runShellScript",
      "Result": {
        "pluginID": "",
        "pluginName": "",
        "status": "",
        "code": 0,
        "output": null,
        "startDateTime": "0001-01-01T00:00:00Z",
        "endDateTime": "0001-01-01T00:00:00Z",
        "outputS3BucketName": "",
        "outputS3KeyPrefix": "",
        "stepName": "",
        "error": "",
        "standardOutput": "",
        "standardError": ""
      },
      "Id": "aws:runShellScript"
    }
  ],
  "CancelInformation": {
    "CancelMessageID": "",
    "CancelCommandID": "",
    "Payload": "",
    "DebugInfo": ""
  },
  "IOConfig": {
    "OrchestrationDirectory": "/var/lib/amazon/ssm/i-0a2ce926164e897c6/document/orchestration/a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae",
    "OutputS3BucketName": "",
    "OutputS3KeyPrefix": "a042e1f0-5fa6-4bf1-bf6c-1c64cf79c2ae/i-0a2ce926164e897c6",
    "CloudWatchConfig": {
      "LogGroupName": "",
      "LogStreamPrefix": "",
      "LogGroupEncryptionEnabled": false
    }
  },
  "UpstreamServiceName": "MessageGatewayService"
}

ということで、Run Commandで渡すコマンド上でPPIDを取得して、SSM AgentのログからCommand IDを取得することができそうです。

実際のCommand IDを取得するコマンドは以下のとおりです。

sleep 3

run_command_pid=$(echo "$PPID")

echo PPID : "$run_command_pid"

command_id=$(grep "\"Pid\":$run_command_pid" /var/log/amazon/ssm/amazon-ssm-agent.log \
  | awk '{print $5}' \
  | tr -d []
)

echo Command ID : "$command_id"

実行すると、以下のような結果が出力されます。

PPID : 1997
Command ID : ba9b8afe-e84f-4eab-985b-67cc11ee3720

ポイントはsleepです。sleepなりで待機しなければ、まだログが出力されていないようで上手く動作することができませんでした。以下のようにCommand IDを取得できるまではループする形でも良いと思います。

run_command_pid=$(echo "$PPID")

echo PPID : "$run_command_pid"

while true; do
  command_id=$(grep "\"Pid\":$run_command_pid" /var/log/amazon/ssm/amazon-ssm-agent.log \
    | awk '{print $5}' \
    | tr -d []
  )

  if [[ -n "$command_id" ]]; then
    break
  else
    sleep 1
  fi
done

echo Command ID : "$command_id"

SSM AgentのログファイルからPPIDで検索して抽出

SSM Run CommandのターゲットのEC2インスタンス上でCommand IDを取得してみました。

Run Command実行ごとに一意のファイルを作成したい場合にはファイル名のサフィックスにCommand IDを付与すると一目で、どのRun commandで生成されたファイルなのか分かりやすそうですね。

「Command IDを使ってこねくり回したい」という方はぜひ、試してみてください。

この記事が誰かの助けになれば幸いです。

以上、AWS事業本部 コンサルティング部の のんピ(@non____97)でした!

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.