【问题标题】:Python on Crontab does not execute bash scriptCrontab 上的 Python 不执行 bash 脚本
【发布时间】:2018-12-06 21:53:21
【问题描述】:
import subprocess as sub
import re
import os
from datetime import datetime as influx_timestap
from influxdb import InfluxDBClient
from collections import OrderedDict

insert_json = []
hostname = str(sub.check_output('hostname')).strip()
location = str(sub.check_output(['ps -ef | grep mgr'], shell=True)).split()
current_dir = os.getcwd()
print("script executed")
gg_location_pattern = re.compile(r'mgr\.prm$')
gg_process_pattertn = re.compile(r'^REPLICAT|^EXTRACT')
for index in location:
    if gg_location_pattern.search(index) != None:
        gg_location = index[:-14]

os.chdir(gg_location)
print("checkpoint1")
get_lag = sub.check_output(str(current_dir) + '/ggsci_test.sh', shell=True)
print("checkpoint2")
processes = get_lag.split("\n")

for process in processes:
    if gg_process_pattertn.search(process) != None:
        lag_at_chkpnt = int((process.split()[3]).split(":")[0]) * 3600 + int((process.split()[3]).split(":")[1]) *60 + int((process.split()[3]).split(":")[2])
        time_since_chkpnt = int((process.split()[4]).split(":")[0]) * 3600 + int((process.split()[4]).split(":")[1]) *60 + int((process.split()[4]).split(":")[2]
)
        process_dict = OrderedDict({"measurement": "GoldenGate_Mon_" + str(hostname) +  "_Graph",
                        "tags": {"hostname": hostname, "process_name": process.split()[2]},
                        "time": influx_timestap.now().isoformat('T'),
                        "fields": {"process_type": process.split()[0], "process_status": process.split()[1],
                        "lag_at_chkpnt": lag_at_chkpnt, "time_since_chkpnt": time_since_chkpnt}})
        insert_json.append(process_dict)


host = 'xxxxxxxx'
port = 'x'
user = 'x'
password = 'x'
dbname = 'x'
print("before client")
client = InfluxDBClient(host, port, user, password, dbname)
client.write_points(insert_json)
print("after client")

此代码手动完美运行,但在 crontab 上无法正常工作。在互联网上搜索后,我发现他们说在 crontab 上更改或设置您的“PATH”变量。我更改了我的“PATH”变量,但它仍然无法正常工作。

Crontab 日志文件写入“checkpoint1”之后就什么都没有了。所以,线路不工作是"get_lag = sub.check_output(str(current_dir) + '/ggsci_test.sh', shell=True)"

之后我可以在这里做什么? 保重,

【问题讨论】:

  • 如何在 crontab 中调用这个脚本?
  • * * * * * /bin/python /gecici/GoldenGate/GoldenGate_Mon/commands_graph.py >> /gecici/GoldenGate/GoldenGate_Mon/x.log
  • 当我在相关行之前添加 try-except 时,我得到以下结果:Command 'ggsci

标签: python bash cron


【解决方案1】:

您的外部脚本 (ggsci_test.sh) 似乎在路径/一般故障方面存在一些问题。

来自Python subprocess documentation关于subprocess.check_output

如果返回码不为零,则会引发 CalledProcessError。这 CalledProcessError 对象将在 returncode 中有返回码 属性和输出属性中的任何输出。

这就是为什么您在捕获错误时看到错误但无法继续的原因。

因此,您应该检查您的 shell 脚本之前是否有任何需要解决的问题。

【讨论】:

  • 您好,我发现了错误,但我不知道如何修复它。当我使用子进程时,我必须在 subprocess.check_output 方法中使用“shell=True”参数。然而,我们的 crontab 不允许我们使用 shell=True。因此,我应该使用 subprocess.Popen,但此时我必须使用“ksh”来运行我的 bash 脚本文件。最后,当我由于 ggsci 命令运行“ggsci
猜你喜欢
  • 2013-04-19
  • 2014-08-06
  • 2020-03-16
  • 2012-09-14
  • 1970-01-01
  • 1970-01-01
  • 2016-07-19
  • 2015-10-14
相关资源
最近更新 更多