【发布时间】: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