【问题标题】:Looking for a way to de-reference a bash var wrapped in a python command call寻找一种方法来取消引用包装在 python 命令调用中的 bash var
【发布时间】:2016-07-19 21:19:09
【问题描述】:

我正在尝试找到一种方法来取消引用 GoldenClusterID,以便在 AWS CLI 命令中使用它来终止我的集群。该程序是为了补偿每天生成的动态作业流编号,因此可以应用按计划正常关闭数据管道。我可以整天 os.system("less goldenClusterID") 它给了我正确的答案。但是,它不会放弃直接取消引用的好东西。有什么建议吗?

from __future__ import print_function

import json
import urllib
import boto3
import commands
import os
import re
import datetime
import awscli

foundCluster = ""
rawClusterNum = ""
mainClusterNum = ""
goldenClusterID = ""

#   Next, we populate the list file with clusters currently active
os.system("aws emr list-clusters --active >> foundCluster")
#   We search for a specific Cluster Name
os.system("fgrep 'AnAWSEMRCluster' foundCluster")
os.system("grep -B 1 DrMikesEMRCluster foundCluster >> rawClusterNum")
#   Look for the specific Cluster ID in context with it's Cluster Name
os.system("fgrep 'j-' rawClusterNum >> mainClusterNum")
#   Regex the Cluster ID from the line
os.system("grep -o '\j-[0-9a-zA-Z]*' mainClusterNum >> goldenClusterID")
#   Read the Cluster ID from the file and run AWS Terminate on it
os.system("aws emr describe-cluster --cluster-id %s" % goldenClusterID")
os.system("aws emr terminate-clusters --cluster-ids goldenClusterID")
os.system("rm *")

【问题讨论】:

  • 将 Python 用于看起来很像 Bash 脚本的东西似乎很奇怪。否则open("goldenClusterID").read()。您要查找的goldenClusterID 是一个文件。
  • @F.X.通常,我只会使用 BASH 并设置一个 cron 来运行它,但是,这必须包含在带有调度程序的 AWS Lambda 中,所以我从 AWS 中的选择不能包括直接的 BASH。因此将其包装在 python 中。

标签: python linux bash amazon-web-services emr


【解决方案1】:

没关系,我想通了。咖啡太多,睡眠不足。答案是使用: goldkeyID=open('goldenClusterID', 'r').read() os.system("aws emr describe-cluster --cluster-id %s" % goldkeyID)

【讨论】:

    猜你喜欢
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 2015-10-25
    • 1970-01-01
    • 2013-03-09
    • 2018-06-25
    • 1970-01-01
    相关资源
    最近更新 更多