我有一个cronjob集,每天运行00:00 GMT。这个cronjob执行一个shell脚本,内部调用python脚本.python脚本生成一个日志文件作为输出.shell脚本和python脚本位于同一个unix框中的目录(/ abc / logs / env / pqr / hvi /)。他们有777作为权限。现在,当我从这个位置运行shell脚本(/ abc / logs / env / pqr / hvi /)时,它执行正常,内部的python脚本被调用&生成日志文件。但是当通过cronjob执行此操作时,输出文件未生成,我收到一条错误消息,指出“AttributeError:'str'对象没有属性'format'” 当我做哪个python 时,我得到:somelocation /../ thirdparty / python / 2.6 / bin / python 所以我认为我使用的是支持 .format 操作的python 2.6。 我的猜测是:
我在下面添加到cronjob的Python解释器问题:
PYTHONPATH = / .. / .. / .. / thirdparty / python / 2.6 / bin / python PATH = / USR / bin中:/ bin中:/ usr / sbin目录:/ sbin目录:在/ usr / local / bin中
但没有运气
不确定问题出在哪里。有人可以帮忙吗? 提前谢谢。
Cron Job看起来像:
PYTHONPATH =/cs/../../thirdparty/python/2.6/bin/python
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
00 00 * * 2,3,4,5,6 /abc/logs/env/pqr/hvi/calculate_latency.sh > /var/tmp/hvi_latency.log 2>&1
Shell脚本如下所示:
#!/usr/bin/ksh
cd /abc/logs/env/pqr/hvi/
NOW=$(date +"%F")
FILE="hvi-latency-$NOW.log"
zgrep "Received message:" $(date -d '-2 day' '+%Y%m%d')*.hvi.data.log.gz $(date -d'yesterday' +%Y%m%d)/*.data.* | cut -d' ' -f1,2,20,21,22 | sed 's/2016.*gz://' | sed 's/ /|/2' > $FILE
chmod 777 $FILE
python /abc/logs/env/pqr/hvi/HVI_Delay_analyizer_BY_HOUR_usingDatetime.py $FILE /abc/logs/env/pqr/hvi/
chmod 777 HVI_MaxDelayPerHOUR.log
cut -d "|" -f2 HVI_MaxDelayPerHOUR.log >HVI_MaxDelay.log
chmod 777 HVI_MaxDelay.log
tail -n +2 HVI_MaxDelay.log > HVI_Latency.log
chmod 777 HVI_Latency.log
cat HVI_Latency.log > HVI_Latency.txt
chmod 777 HVI_Latency.txt
mail -s "DR Latency values for HVI feeder on date:$NOW" sajida.sayyad@gmail.com < HVI_Latency.txt
python脚本如下所示:
import os
import sys
import time
import math
import re
from datetime import datetime
from datetime import timedelta
def get_timestamp():
import datetime
n = datetime.datetime.now()
# print n gives : 2016-03-25 04:08:43.123162
result = '{0:04}-{1:02}-{2:02}_{3:02}{4:02}{5:02}'.format(n.year,n.month,n.day,n.hour,n.minute,n.second)
# print result gives :2016-03-25_040843
return result
错误讯息为:
[..... tmp]$ tail -100 hvi_latency.log
2016-03-25 05:31:43.806450
Traceback (most recent call last):
File "/abc/logs/env/pqr/hvi/HVI_Delay_analyizer_BY_HOUR_usingDatetime.py", line 119, in ?
START_TIME = get_timestamp()
File "/abc/logs/env/pqr/hvi/HVI_Delay_analyizer_BY_HOUR_usingDatetime.py", line 18, in get_timestamp
result = '{0:04}-{1:02}-{2:02}_{3:02}{4:02}{5:02}'.format(n.year,n.month,n.day,n.hour,n.minute,n.second)
AttributeError: 'str' object has no attribute 'format'