Amazon Linux实例在使用subprocess.run的Python脚本中死亡

Amazon Linux实例在使用subprocess.run的Python脚本中死亡,python,linux,amazon-web-services,Python,Linux,Amazon Web Services,我试图通过子流程模块在Amazon Linux 2016.09 c4.xlarge上运行一些命令 我不能使用chef或其他CM工具,因为在运行脚本之前我不知道这些命令。我的脚本解析从CIS安全工具输出的XML报告 在一个实例中,在尝试以下语句后,我得到了错误: executing command chown root:root /etc/cron.daily executing command iptables -P INPUT DROP executing command yum -y

我试图通过
子流程
模块在
Amazon Linux 2016.09 c4.xlarge
上运行一些命令

我不能使用
chef
或其他
CM工具
,因为在运行脚本之前我不知道这些命令。我的脚本解析从CIS安全工具输出的XML报告

在一个实例中,在尝试以下语句后,我得到了错误:

executing command  chown root:root /etc/cron.daily
executing command  iptables -P INPUT DROP
executing command  yum -y install aide

# then crashes: terminal freezez and instance is inaccessible over port 22
在另一次审判中,令人不快的判决是

executing command  chown root:root /etc/cron.daily
executing command  iptables -P INPUT DROP
executing command  yum -y remove xorg-x11*
Loaded plugins: priorities, update-motd, upgrade-helper
Resolving Dependencies
--> Running transaction check
---> Package xorg-x11-font-utils.x86_64 1:7.2-11.5.amzn1 will be erased
---> Package xorg-x11-fonts-Type1.noarch 0:7.2-9.1.5.amzn1 will be erased
--> Processing Dependency: xorg-x11-fonts-Type1 for package: 1:java-1.7.0-openjdk-1.7.0.131-2.6.9.0.71.amzn1.x86_64
--> Running transaction check
---> Package java-1.7.0-openjdk.x86_64 1:1.7.0.131-2.6.9.0.71.amzn1 will be erased
--> Finished Dependency Resolution

Dependencies Resolved
# then crashes: terminal freezez and instance is inaccessible over port 22
脚本本身是:

class CommandFix(object):

    def _execute_command(self):
        for command in self.commands:
            command = command.replace('#', '')
            command = command.replace('yum', 'yum -y')

            print("executing command", command)

            self.stdout_log.write("fixing {} : running command {}\n".format(
                self.rule_id,
                command))

            try:
                cp = subprocess.run(command,
                    shell=True, check=True
                    )
            except subprocess.CalledProcessError as e:
                self.stderr_log.write("error fixing {} : {}\n".format(
                    self.rule_id,
                    e))

                # TO-DO: Handle error with retry or similar
                # right now just return false
                return False

        return True

您可能需要关闭子流程

尝试在代码中添加以下行-

import os
process_execution_status = False
try:
    #your code
    process_execution_status = True
catch Exception as exc:
    #exc handling
    process_execution_status = False
finally:
    os.killpg(os.getpgid(cp.pid), signal.SIGTERM)
    return process_execution_status

您可能需要关闭子流程

尝试在代码中添加以下行-

import os
process_execution_status = False
try:
    #your code
    process_execution_status = True
catch Exception as exc:
    #exc handling
    process_execution_status = False
finally:
    os.killpg(os.getpgid(cp.pid), signal.SIGTERM)
    return process_execution_status

问题在于
iptables
命令。它正在删除所有入站连接。实例很好,只是被iptables阻塞了

问题在于
iptables
命令。它正在删除所有入站连接。实例很好,只是被iptables阻止了

我想你是对的!我现在就来试试,然后在这里报告答案就在眼前!iptables!我把自己挡在外面了好吧,但我认为关闭子流程也很重要。如果你觉得它有用,请投它一票!我想你是对的!我现在就来试试,然后在这里报告答案就在眼前!iptables!我把自己挡在外面了好吧,但我认为关闭子流程也很重要。如果你觉得它有用,请投它一票!