Python脚本只能通过cronjob部分执行

时间:2015-10-24 19:24:32

标签: python cron partial

大家好我试图搜索论坛以获得答案,但无法找到与我的问题相关的任何内容。我有一个由cronjob执行的.sh文件。在.sh文件中,我启动了一个python脚本。 python脚本运行但随后停止在脚本的特定部分。当我从命令行以root身份运行时,python脚本正常运行。它出现在cron作业时,它只是部分运行它。我现在将在下面发布我的python脚本。任何有关这方面的帮助将不胜感激。我已在脚本停止运行的脚本上进行了标记。 谢谢

#!/usr/bin/python
import re
import mechanize
import datetime
import csv
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import pyodbc
from time import sleep



def current_date(x = 0):
    a = datetime.datetime.now()
    b = datetime.timedelta(days=x)
    c = a - b
    return c.strftime("%Y-%m-%d")


def logger(x,sql,timey):
    file = open(x, 'a+')
    file.write(sql + "\n" + timey.strftime("%H:%M:%S")+"\n")
    file.close()



def edam(sql):
    cnxn = pyodbc.connect("Driver=OracleODBC-    10g;Server=xxx;PORT=1521;UID=xxxxx;PWD=xxxxx")
    cursor = cnxn.cursor()
    cursor.execute(sql) 
    cursor.commit()
    cnxn.close()
    logger("/home/api06/python/sms_"+ a.strftime("%Y-%m-%d"),sql,a)

a = datetime.datetime.now()

path= "/home/api06/python/"
txt = "sms_" + a.strftime("%Y")+ "_" + a.strftime("%m")+ "_"  + a.strftime("%d") + ".csv"


usr="xxxxx"
passy="xxxx@2xx0xx12"
br = mechanize.Browser()
br.open("https://sms.xxxxxxxx.com/login/auth")
br.select_form(nr=0)
br.form["j_username"] = "xxxxxxx" # (the method here is __setitem__)
br.form["j_password"] = "xxxxxxx@2x0xxxxx12" # (the method here is __setitem__)
response2 = br.submit()
response = br.follow_link(text="SMS Log")
x = 0
br.retrieve('https://sxxxxxxms.xxxxxxxx.com/sms/list?format=csv&extension=csv',path+txt )[0]

此点之后的所有内容都没有运行

edam("TRUNCATE TABLE ap_sms_test")
with open(path+txt, 'rb') as f:
    reader = csv.reader(f)
    your_list = list(reader)



x = len(your_list)

for y in range(0,x):
    your_list[y][4] = your_list[y][4][0:19]



dt = DataFrame(your_list)
dt = dt.drop(5,1)
dt = dt.drop(6,1)
dt = dt.ix[1:]
dt.is_copy = False
dt[4] = dt[4].astype('datetime64[ns]')
newone = dt[4] > current_date(5)
dt.loc[newone]
dt = dt.loc[newone]
new_list = dt.values.T.tolist()


cnxn = pyodbc.connect("Driver=OracleODBC-10g;Server=xxxxx;PORT=1521;UID=xxxxx;PWD=xxxxx")
cursor = cnxn.cursor()



j = 0 
try:
    for row in dt.index:
        bee = str(new_list[2][j])
        bee = bee.replace("'","")

        j = j + 1   


    #cursor.execute("INSERT INTO ap_sms_test VALUES ('" + str(dt[0].loc(row)) + " ','" +  str(dt[1].loc(row)) + "','" + bee + "','" +str(dt[3].loc(row)) + "','" + str(dt[4].loc(row)) +"')")
#cursor.execute("INSERT INTO ap_sms_test VALUES ('" + {} + " ','" +  {} + "','" + bee + "','" + {} + "','" + {} +"')".format(str(new_list[0][j]),str(new_list[1][j]),str(new_list[3][j]),str(new_list[4][j])))
    cursor.execute("INSERT INTO ap_sms_test VALUES ('" + str(new_list[0][j]) + " ','" +  str(new_list[1][j]) + "','" + bee + "','" + str(new_list[3][j]) + "','" + str(new_list[4][j]) +"')")
    except:
        pass     
cursor.commit()
cnxn.close()

sql="""CREATE TABLE SVBO_SMS_SENT_TEMP2 AS(SELECT
 something
FROM
  ap_sms_test a)"""


edam(sql)
edam("DELETE FROM SMS_SENT WHERE TRUNC(DATE_SENT)>= TRUNC(SYSDATE)-5")   
edam("INSERT INTO SMS_SENT (SELECT * FROM SMS_SENT_TEMP2       WHERETRUNC(SENT_DATE)>= TRUNC(SYSDATE)-5)")
edam("DROP TABLE SMS_SENT_TEMP2;")
edam("UPDATE JOBS set JOBS_RUN_DATE = SYSDATE WHERE JOB_NAME ='SMS Sent'")

0 个答案:

没有答案