2011-12-20 89 views
0

我使用配置解析器解析一个配置文件,我需要尾巴在我使用的是Mac打开一个子进程以配置文件中的每个文件?

我写的代码获取我的文件部分所有的东西蟒蛇一个单独的终端中的文件部分中的每个文件I仅需要的路径,并需要尾它们中的每一个在一个单独的子进程

import ConfigParser 

    import os 

def ConfigSectionMap(section): 
    dict1 = {} 
    options = Config.options(section) 
    for option in options: 
     try: 
      dict1[option] = Config.get(section, option) 
      if dict1[option] == -1: 
       DebugPrint("skip: %s" % option) 
     except: 
      print("exception on %s!" % option) 
      dict1[option] = None 
    return dict1 

Config = ConfigParser.ConfigParser() 
Config.read("/etc/harvest.conf") 
print Config.sections() 
print ConfigSectionMap("files") 

样本配置文件是

[SECTION1] host_prefix =真

timestamp_prefix =真

[第2节]主机=本地主机

端口= 1463

的PID =的/ var /运行/收割机

[文件] apache.access = /无功/日志/的apache2/access.log的

apache.errors = /var/log/apache2/errors.log

邮件= /var/log/mail.log

mysql.log = /var/log/mysql.log

mysql.err = /var/log/mysql.err

syslog.err = /var/log/syslog.err

回答

1

由于tail正在接受多个文件,你可以依靠它:

from ConfigParser import ConfigParser 
from subprocess import Popen 

config = ConfigParser() 
config.read('/etc/harvest.conf') 
filenames = [value for name, value in config.items('files')] 

process = Popen(['tail', '-f'] + filenames) 
process.communicate() 
+0

将每个文件都有它自己的进程? – Rishabh 2011-12-20 17:01:48

+0

不,但尾巴可以在同一时间“拖尾”所有文件。如果您想为每个文件名执行此操作,请对其进行修改并循环。 – tito 2011-12-21 00:56:23

+0

适应?我怎么做? – Rishabh 2011-12-21 06:53:48