在AWS实例关闭/终止期间运行脚本

时间:2018-07-10 22:04:01

标签: shell amazon-web-services amazon-ec2 debian

我需要在关闭/终止AWS实例(debian)期间执行脚本。

我将脚本添加到#include <stdio.h> #include <string.h> /* strlen() */ #include <ctype.h> /* isdigit() */ int main() { int n, invalidInput, status; char strInput[5]; int i; /* for loop */ do { invalidInput = 0; printf("Enter a **number** between 1 and 100: "); fflush(stdin); status = scanf("%4s", strInput); /* read at maximum 4 chars from stdin */ if (status > 3) { continue; /* try again */ } for (i = 0; i < strlen(strInput); ++i) { /* ensure that all characters are numbers */ if (!isdigit(strInput[i])) { invalidInput = 1; break; } } if (!invalidInput) { n = atoi(strInput); /* now your input is a integer */ if (n < 1 || n > 100) { printf("Error: the input must be between 1 and 100.\n"); invalidInput = 1; } } } while (invalidInput); /* repeat until all chars are digits and they are in the required interval */ return 0; } ,并且将符号链接添加到/etc/init.d/myscript,但是我注意到,在终止实例时,我看不到脚本已执行。

有什么主意吗?

1 个答案:

答案 0 :(得分:1)

我不希望Amazon正常终止实例。在正常的操作系统关闭期间,请确保您可以运行kill脚本。但不是终止。如果您有兴趣收到有关终止通知的信息,请查看将EC2 CloudWatch events附加到实例。根据文档,您可以获得:

  

此EC2实例状态更改通知事件的示例显示   实例处于挂起状态。状态的其他可能值   包括运行,关闭,停止,停止和终止。

带有JSON包,例如:

{
   "id":"7bf73129-1428-4cd3-a780-95db273d1602",
   "detail-type":"EC2 Instance State-change Notification",
   "source":"aws.ec2",
   "account":"123456789012",
   "time":"2015-11-11T21:29:54Z",
   "region":"us-east-1",
   "resources":[
      "arn:aws:ec2:us-east-1:123456789012:instance/i-abcd1111"
   ],
   "detail":{
      "instance-id":"i-abcd1111",
      "state":"pending"
   }
}

如果您确实需要处理此问题,则必须先关闭操作系统,这将运行您的脚本,然后终止已停止的实例。

相关问题