gpt4 book ai didi

python - 将子进程通过管道传递到 Linux 中的文件

转载 作者:太空宇宙 更新时间:2023-11-04 09:22:22 26 4
gpt4 key购买 nike

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

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

我可以直接从 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()

最佳答案

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 的程序。

关于python - 将子进程通过管道传递到 Linux 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40490181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com