我有这个脚本比我从命令行运行它工作得很好。。。在
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
from time import sleep
import os
import logging
import glob
import sys
import subprocess
import pymssql
import shutil
import StringIO
from StringIO import StringIO
#Check share mounted
if os.path.ismount("/home/marco/cas01-share/") == 0:
#logging.info('share not mounted')
print "share not mounted"
#mount share
#logging.info('share now mounted')
#print "share now mounted"
else:
print "share mounted"
# File paths
fullpath2Newfile = "/home/marco/cas01-share/New/"
fullpathfiles2Process = "/home/marco/cas01-share/Process/"
fullpathProcessedfiles = "/home/marco/cas01-share/Processed/"
if not os.listdir('/home/marco/cas01-share/New'):
print "directory empty"
else:
print "directory not empty"
# Directory more than one file?
num_files = len([f for f in os.listdir('/home/marco/cas01-share/New')
if os.path.isfile(os.path.join('/home/marco/cas01-share/New', f))])
print "directory has " + str(num_files) + " files"
num_files = 0
for filename in os.listdir('/home/marco/cas01-share/New'):
succesfullRun = 0
num_files += 1
print filename
filename = str(filename)
batchnumber = filename.strip("['SENSE-extract-ascii--.csv']")[:-9]
print batchnumber
newfullpath2Newfile = fullpath2Newfile + filename
fullpathfiles2Process = "/home/marco/cas01-share/Process/SENSE-extract.csv"
fullpathProcessedfiles = "/home/marco/cas01-share/Processed/SENSE-extract-" + batchnumber + ".csv"
print newfullpath2Newfile
print fullpathfiles2Process
print fullpathProcessedfiles
# Execute job
ExceptionFlag = 0
while (succesfullRun == 0):
if ExceptionFlag == 0:
shutil.move(newfullpath2Newfile, fullpathfiles2Process)
try:
# Move to sense check folder
# Connect to DB
conn = pymssql.connect(server='IP', user='User', password='password', tds_version='8.0')
cursor = conn.cursor()
cursor.execute("EXEC msdb.dbo.sp_start_job @job_name = 'Marco_test'")
except Exception as e:
(error_code, error_message) = e
ExceptionFlag = 1
#print error_message
if "does not exist" in error_message:
print "ERROR: Job name not found"
if "job is already running" in error_message:
print "ERROR: a Job is already running"
if "job already has a pending request" in error_message:
print "ERROR: pending request"
time.sleep(0.5)
else:
print "Job executed succesfully"
# Copy file to processed
shutil.copy(fullpathfiles2Process, fullpathProcessedfiles)
succesfullRun = 1
conn.close()
time.sleep(0.5)
exit()
但是当我试图在crontab上进行计划时,我得到了如下错误
^{pr2}$我得到这个错误:
Traceback (most recent call last): File "...", line 239, in (error_code, error_message) = e ValueError: need more than 1 value to unpack
我认为这是因为“cronos schedule executed job”和它自己的脚本之间没有管道,我不知道这是否有意义,就像它不知道从何处获取信息和viceversa。。。在
本质上是在试图捕捉执行MSSQL作业命令的异常。在
你的问题是你正在解包一个异常。不应假定异常是ITerable,也不应假定它们包含参数。在
如果您将消息传递给异常类,则它的值为1:
^{pr2}$错误处理固有的会在错误捕获后产生错误。学习使用回溯或
Exception.args
代替:回溯示例
异常.args示例
更新和工作代码:
相关问题 更多 >
编程相关推荐