我试图通过在配置文件中允许自定义系统调用来允许自定义监控。这是一个简单的 .ini 文件,但我对得到的结果有疑问。
def sys_command(self, command):
command_result = subprocess.check_output(["stat -c \"%s\" ./endpoint.py"], shell=True)
return command_result
以上工作正常。
def sys_command(self, command):
command_result = subprocess.check_output([command], shell=True)
return command_result
以上是解析命令时不起作用的内容:
[directory]
file_size = "stat -c \"%s\" {file}"
其他函数处理 {file} 的解析、映射和字符串替换。我检查了传入的命令,它是有效的。这是我遇到的错误。
"stat -c \"%s\" ./endpoint.py"
/bin/sh: stat -c "%s" ./endpoint.py: No such file or directory
Traceback (most recent call last):
File "monitor.py", line 147, in <module>
print(test.custom_sys_command(test_focus_dict, "directory"))
File "monitor.py", line 63, in custom_sys_command
cmd_results = self.sys_command(parsed_cmd)
File "monitor.py", line 70, in sys_command
command_result = subprocess.check_output([command], shell=True)
File "/usr/lib64/python2.7/subprocess.py", line 575, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['"stat -c \\"%s\\" ./endpoint.py"']' returned non-zero exit status 127
第一行是解析出来传入的命令。
subprocess.CalledProcessError: Command '['"stat -c "%s" ./endpoint.py"']' returned non-zero exit status 127
以上是我删除配置文件中的转义字符时出现的错误。
最佳答案
删除 .ini 文件中的引号解决了我的问题。
“check_output 没有添加撇号,.ini 解析器将它们视为文字字符。”
关于python - Python 系统调用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49561198/