ec2 启动时间、定时关闭ec2

[root@ip-10-254-25-232 ~]# cat /server/scripts/stop_myself.sh
#!/bin/bash

# 获取时间戳
local_time=$(date +%s)

# 获取 ec2 启动时间
ec2_start_time=$(aws --profile hk ec2 describe-instances --instance-id i-03c7ff59cf846a7c7 --query 'Reservations[*].Instances[*].{LaunchTime:LaunchTime}' --output json | awk -F "[\"]+" '/LaunchTime/ {print $(NF-1) }')
# 将ec2启动时间转换为 时间戳
ec2_start_time_d=$(date -d "${ec2_start_time}" +%s)

# 判断 启动时间,大于1小时执行关机  命令
if [ $((${local_time}-${ec2_start_time_d})) -ge 3600 ];then
	echo yes stop ec2
        /usr/bin/aws --profile hk ec2 stop-instances --instance-ids i-03c7ff59cf846a7c7
else
	echo no
fi
posted @ 2022-08-01 12:40  Star-Hitian  阅读(136)  评论(0编辑  收藏  举报