zoukankan      html  css  js  c++  java
  • Linux- AWS之EC2大数据集群定时开关机

      众所周知,云计算就是在计算你的钱,每当ec2开起来就要开始计费。当用户购买了一个庞大的与服务器做一个集群,尤其是用来做大数据集群,这些服务器的配置相当高,每台服务器所需要的费用不菲。其实在很多时候没能够完全利用起其全部的资源,尤其在空闲时间,在夜间没有作业的情况下,这些服务器完全处于空闲的状态,却时刻在计费,这是相当不划算的。于是有这样一个方案,我们是不是可以在机器处于空闲的状态时将他们关闭,第二天上班前将他们开起来,下班后不需要使用时又将它们关机。

      在写好脚本运行之前,最好检查一遍开机自启动列表,看看你的业务服务是否又设置了开机自启动,如果没有的话你需要考虑是将它开启还是在脚本上设置每次开机后都开启服务了。相关命令:chkconfig

     Python script

    开机脚本

    # coding=utf8
    import boto3
    ec2 = boto3.resource('ec2')
    ids = ['i-00000000000000000','i-00000000000000001', 'i-00000000000000002', 'i-00000000000000003','i-00000000000000004','i-00000000000000005','i-00000000000000006']
    ec2.instances.filter(InstanceIds=ids).start()
    

     关机脚本

     # coding=utf8
    import boto3
    ec2 = boto3.resource('ec2')
    ids = ['i-00000000000000000','i-00000000000000001', 'i-00000000000000002', 'i-00000000000000003','i-00000000000000004','i-00000000000000005','i-00000000000000006']
    ec2.instances.filter(InstanceIds=ids).stop()
    

     Shell script

    开机脚本

    #!/bin/bash
    export PATH=/usr/local/python34/bin/:$PATH
    export PATH=~/.local/bin:$PATH
    echo test-ec2sh >> /user/teststartec2.txt
    python3 /user/pyfile/startec2.py
    

     关机脚本

    #!/bin/bash
    export PATH=/usr/local/python34/bin/:$PATH
    export PATH=~/.local/bin:$PATH
    python3 /user/pyfile/stopec2.py
    

     关机脚本并且打印日志

    #!/bin/bash
    TMP_A=/tmp/awsstop.check
    TMP_B=/user/shfile/checkfile/awsstop.standerd
    TMP_C=/tmp/awsstop.log
    rm -rf TMP_A
    sleep 600
    for (( i=1;i<=30;i++ ))
    do
    DIFF=$(diff $TMP_A $TMP_B)
    if [[ -z $DIFF ]];
     then
       echo "Can stop aws"
       echo "Can stop aws" > $TMP_C
       date >> $TMP_C
      /user/shfile/stopec2.sh
    else
       echo "Can not stop aws waiting 30 minute..."
       echo "Can not stop aws" > $TMP_C
       date >> $TMP_C
       sleep 1800
    fi
    done
    

     crontab 定时执行请参考我的另一篇博文:Linux- Linux自带定时调度Crontab使用详解

    Linux- Linux自带定时调度Crontab使用详解
    Linux - Linux自带定时调度Crontab使用详解
  • 相关阅读:
    Ibatis中SqlMapClientTemplate和SqlMapClient的区别
    Spring整合Ibatis之SqlMapClientDaoSupport
    iBatis查询时报"列名无效"或"找不到栏位名称"无列名的错误原因及解决方法
    五个程序员求职者的最佳提问
    JVM性能调优指南
    Review software requirements specification and create test scenarios (what to test for a certain functionality)
    Security Testing Test Scenarios
    Performance testing test scenarios
    Test Scenarios for Excel Export functionality
    Test Scenarios for sending emails
  • 原文地址:https://www.cnblogs.com/RzCong/p/8356143.html
Copyright © 2011-2022 走看看