将子流程管道传输到Linux

时间:2016-11-08 15:02:33

标签: python linux bash perl

我试图让我的代码正常工作,我的头靠在墙上。本质上,我编写了一个将由cron调用的python脚本。该脚本每周运行一次以执行数据库维护。我的python脚本将调用执行实际维护的perl脚本。我必须以这种方式构建它,因为perl脚本将共运行9次。有3种不同类别的数据库项目,每个类别需要perl脚本中的3种不同操作。如果任何类别中的单个操作失败,则脚本将从该类别中断并移至下一个类别(每个操作都取决于之前的操作)。

我的脚本无法正常执行,我不确定原因。我正在尝试对其进行故障排除,但我的日志记录逻辑也无法正常工作。我在日志文件中得到的唯一输出是简单的" print"我写入脚本中用于表示法和格式化的命令。

我可以直接在CLI中运行perl脚本,但是从我的python脚本调用时无法正常运行。有人能指出我正确的方向吗?

编辑: 在回答一个问题时,我没有很好地解释perl脚本是如何失败的。我没有从perl脚本中将stdout或stderr放入我的日志文件中,因此我还不知道它为什么会失败。 我没有创建perl脚本,它提供了我管理的软件套件。

这里是日志文件输出:

------------------------------------------------------------------------------------------------------
Weekly Voyence archival, deletion, and purging of jobs, events, and revisions beginning at 14:46:11
Block failed, continuing to next block
------
Block failed, continuing to next block
------
Block failed, continuing to next block
------
Weekly Voyence archival, deletion, and purging of jobs, events, and revisions complete at 14:46:11

以下是从CLI运行perl脚本的输出(没有错误):

Script started at Mon Nov  7 14:59:53 2016

Archving/Pruning 0 Parent jobs older than 90 days...
Archving/Pruning 0 Child Jobs older than 90 days...

No Records avaialble for archive..jobs Exiting
Script Completed.

The database utility console log can be found at /opt/voyence/tools/db-   utility/logs/dbutil_console.log

Script exited at Mon Nov  7 14:59:58 2016

这是python脚本:

#! /usr/bin/python
import sys
import time
import os
import subprocess
def SubProcessCaller(PATH,CallLoad,LogObj,LOGFILE):
    try:
        # subprocess.Popen(["/usr/bin/perl", "-i", "/opt/voyence/tools/db-    utility/database-utility.pl", CallLoad], stderr=LogObj.write, stdout=LogObj.write, shell=true)
        concatvar = "/usr/bin/perl -I /opt/voyence/tools/db-utility/common /opt/voyence/tools/db-utility/database-utility.pl" + CallLoad + " >> " + LOGFILE
        subprocess.check_output(concatvar.split(), shell=True)
        return True
    except:
        LogObj.write("Block failed, continuing to next block" "\n")
        return False

if __name__ == "__main__":

    #DECLARE VARIABLES AND OBJECTS
    TIME = time.strftime("%H:%M:%S")
    PATH = "/opt/voyence/tools/db-utility/database-utility.pl"
    DATE = time.strftime("%Y%m%d")
    LOGFILE = "/var/log/voyence-archive/" + DATE + "voyence-archive.log"
    LOG = open(LOGFILE, "a+")
    #CREATE NEW LOG FILE
    LOG.write("------------------------------------------------------------------------------------------------------\n")
    LOG.write("Weekly Voyence archival, deletion, and purging of jobs, events, and revisions beginning at " + TIME + "\n")
    #JOB ARCHIVAL, DELETION, AND PURGING
    JobActionsDict = [" archive jobs 90"," delete jobs 90"," purge jobs 275"]
    #EVENT ARCHIVAL, DELETION, AND PURGING
    EventActionsDict = [" archive events all 90"," delete events all 90"," purge events all 275"]
    #REVISION ARCHIVAL, DELETION, AND PURGING
    RevisionActionsDict = [" archive revisions 40"," purge revisions 60","  delete revisions '3 months' 40"]

    ActionsTuple = (JobActionsDict, EventActionsDict, RevisionActionsDict)

    for dict in ActionsTuple:
        for item in dict:
            SPCBool = SubProcessCaller(PATH,item,LOG,LOGFILE)
            if SPCBool == False:
                break
        LOG.write("------\n")
    #CLOSE AFTER ALL LOGS HAVE BEEN WRITTEN
    LOG.write("Weekly Voyence archival, deletion, and purging of jobs, events, and revisions complete at " + TIME + "\n")
    LOG.close()

2 个答案:

答案 0 :(得分:2)

concatvar = "/usr/bin/perl -I /opt/voyence/tools/db-utility/common /opt/voyence/tools/db-utility/database-utility.pl" + CallLoad + " >> " + LOGFILE

这会将您的程序名称(database-utility.pl)与CallLoad变量的内容连接起来 - 而不会在它们之间留一个空格。因此,除非CallLoad中的值以空格开头,否则您实际上正在寻找名为database-utility.plWhateverIsInCallLoad的程序。

答案 1 :(得分:0)

未经测试,但更具惯用性,Python。主要更改是在调用Perl脚本时完全绕过shell,并记录subprocess.call可能引发的任何错误以帮助调试问题。 (请考虑使用logging模块,但这已有足够的更改。)

#! /usr/bin/python
import sys
import time
import os
import subprocess

def subprocess_caller(path, call_load, log_obj):
    subprocess.call(["/usr/bin/perl",
                     "-I",
                     "/opt/voyence/tools/db-utility/common",
                     path,
                     call_load],
                     stdout=log_obj)

log_header = """\
------------------------------------------------------------------------------------------------------
Weekly Voyence archival, deletion, and purging of jobs, events, and revisions beginning at %s
""" %(time.strftime("%H:%M:%S"),)

if __name__ == "__main__":

    #declare variables and objects
    path = "/opt/voyence/tools/db-utility/database-utility.pl"
    logfile = time.strftime("/var/log/voyence-archive/%Y%m%dvoyence-archive.log")

    #create new log file
    with open(logfile, "a+") as log:
        log.write(hog_header)
        #job archival, deletion, and purging
        job_actions = [" archive jobs 90"," delete jobs 90"," purge jobs 275"]
        #event archival, deletion, and purging
        event_actions = [" archive events all 90"," delete events all 90"," purge events all 275"]
        #revision archival, deletion, and purging
        revision_actions = [" archive revisions 40"," purge revisions 60","  delete revisions '3 months' 40"]

        actions = job_actions + event_actions + revision_action:

        for action_type in actions:
            for item in action_type:
                try:
                    subprocess_caller(path, item, log)
                except Exception as exc:
                    log_obj.write(exc + "\n")
                    log_obj.write("Block failed, continuing to next block\n")
                    break
                log.write("------\n")

        log.write("Weekly Voyence archival, deletion, and purging of jobs, events, and revisions complete at %s\n" %
                  (time.strftime("%H:%M:%S"),))