2016-07-02 2 views
0
내가 잘 작동 명령 줄에서 실행할 때 이상이 스크립트를 가지고

을 제외하고파이썬의 crontab ... ValueError를

#!/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을에 예약 할 때 나는 울부 짖는 오류를 얻을

# Edit this file to introduce tasks to be run by cron. 
# 
# Each task to run has to be defined through a single line 
# indicating with different fields when the task will be run 
# and what command to run for the task 
# 
# To define the time you can provide concrete values for 
# minute (m), hour (h), day of month (dom), month (mon), 
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system 
# daemon's notion of time and timezones. 
# 
# Output of the crontab jobs (including errors) is sent through 
# email to the user the crontab file belongs to (unless redirected). 
# 
# For example, you can run a backup of all your user accounts 
# at 5 a.m every week with: 
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ 
# 
# For more information see the manual pages of crontab(5) and cron(8) 
# 
# m h dom mon dow command 

#SENSE 
*/5 * * * * /usr/bin/python /home/administrator/Share/scripts/python3 >> /home/administrator/Share/scripts/logs/sense$ 

는이 오류 얻을 :

Traceback (most recent call last): File "...", line 239, in (error_code, error_message) = e ValueError: need more than 1 value to unpack

내가 생각하는 그것의 일정 조 실행 "크로노스 사이에 파이프 라인이 없다 함께 할 수있는 b "와 스크립트 자체, 나는 그것이 어떤 정보를 얻고 어디에서 정보를 얻을 수 있는지 알지 못한다면 알 수 없다 ...

EXECUT의 예외를 잡으려고 노력할 때 MSSQL JOB 명령.

+1

그냥 담당자 'print (e)'를 사용하여 오류가 무엇인지 확인하십시오. 그것은 당신이 예상하지 못한 것일 수도 있습니다. 일단 그 버그를 고치면, 예외를 제외하고는 예외를 바꿔서 알려지지 않은 에러가 조용히 전달되지 않도록해야합니다. – zondo

답변

0

업데이트 및 코드 :

#!/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 
import mechanize 
from BeautifulSoup import BeautifulSoup 
import urllib2 

#Configure logging 
if os.path.isdir("/home/administrator/Share/scripts/logs/") == 0: 
    os.makedirs("/home/administrator/Share/scripts/logs/") 

if os.path.isfile("/home/administrator/Share/scripts/logs/sense.log") == 0: 
    open("/home/administrator/Share/scripts/logs/sense.log", 'w') 

logging.basicConfig(filename='/home/administrator/Share/scripts/logs/sense.log',level=logging.DEBUG,format='%(asctime)s - %(message)s') 
logging.info('RUNNING SCRIPT') 

#Check share mounted 
if os.path.ismount("/home/administrator/-share/") == 0: 

    logging.info('share not mounted') 
    #print "share not mounted" 
    #mount share 
    os.popen("sudo mount -t cifs \"//path to network share -o username=username,domain=domain,sec=ntlmssp,password=password") 
    logging.info('share now mounted') 
    #print "share now mounted" 

# start Automated browsing 
browser = mechanize.Browser() 
browser.open("page") 
browser.select_form(nr = 0) 
browser.form['email'] = "user" 
browser.form['password'] = "pass" 
browser.submit() 

#Open Page (Data) 
browser.follow_link(text="Data") 

#Find generate button 
i = 0 
batch = 0 
for form in browser.forms(): 

    browser.form = list(browser.forms())[0] # use when form is unnamed 

    for control in browser.form.controls: 
     if control.name == "generate": 
      batch = 1 
      logging.info('generate detected') 
      #print "generate detected" 
      #Click button 
      browser.submit(nr=i) 

    if batch == 0: 
     logging.info('nothing to generate') 
     #print "nothing to generate" 
     #print "*******************" 

    i =+ 1 
#END Find generate button 

#Back to "admin/Dowload" page (Data) 
browser.follow_link(text="Data") 

# Data Table parsing 
html = browser.response().read() 
soup = BeautifulSoup(html) 

table = soup.find("table") 

i = 0 
for row in table.findAll("tr")[1:]: 

    col = row.findAll("td") 

    if not col: 
     break 

    batch = str(col[0]) 
    records = col[1] 
    generated = col[2] 
    downloaded = col[3] 
    links = col[4] 
    photos = col[5] 
    sheet = str(col[6]) 
    confirmed = col[7] 

    if "Not yet downloaded" in confirmed: 
     i += 1 

     stripbatch = batch.strip("<td></td>") 
     stripsheet = sheet.strip("<td><a href=\"download/xls/\">Excel</a></td>") 

     url = "website" + stripsheet 
     time.strftime("%d/%m/%Y") 
     file = "/home/administrator/-share/New/SENSE-extract-ascii-" + stripsheet + "-" + time.strftime("%Y%m%d") + ".csv" 

     #Dowload file 
     logging.info('File %s Downloaded', stripsheet) 
     #print "File " + stripsheet + " Downloaded" 
     browser.retrieve(url, file)[0] 

    if i < 1: 
     logging.info('nothing to dowload') 
     #print "nothing to dowload" 
     #print "******************" 
     break 

#END Table parsing 

#Reparse table to confirm 
browser.follow_link(text="Data") #reload page to see changes 

html = browser.response().read() 
soup = BeautifulSoup(html) 

table = soup.find("table") 

browser.select_form(nr = 0) 

i=1 
while i >= 1: 

    browser.follow_link(text="Data") #reload page to see changes 

    html = browser.response().read() 
    soup = BeautifulSoup(html) 
    table = soup.find("table") 
    browser.select_form(nr = 0) 

    for form in browser.forms(): 

     for control in browser.form.controls: 
      if control.type == "submit": 
       i += 1 
      else: 
       i = 0 

    if i == 0: 
     logging.info('nothing to confirm') 
     #print "nothing to confirm" 
     #print "******************" 
    else: 
     logging.info('%s confirmed', control.name[0]) 
     #print control.name[0] + "confirmed" 

    browser.submit(nr = 0) 

logging.info('*** Batch generated and CSV file downloaded from Sense ***') 
#print "*** done ***" 

#END reparsin table to confirm 

#Change mount for access to test, for NEW, processed ... 
# Declase File paths 

# Scrap file information for naming Naming 

# Move and replace from New to sense processed folder 

#Execute Job start by job_name 

# Copy file to processed 


#END Sense file downloading 


# File paths 
fullpath2Newfile = "/home/administrator/-share/New/" 
fullpathfiles2Process = "/home/administrator/-share/" 
fullpathProcessedfiles = "/home/administrator/-share/Processed/" 

if not os.listdir('/home/administrator/cas01-share/New'): 
    # print "directory empty" 
     logging.info('No New files to upload') 

else: 
    #print "directory not empty" 

    # Directory more than one file? 
    num_files = len([f for f in os.listdir('/home/administrator/-share/New') 
       if os.path.isfile(os.path.join('/home/administrator/-share/New', f))]) 
    #print "directory has " + str(num_files) + " files" 

    # logging.info('More than one file in New Directory') 
    logging.info('Directory has: %s Files', str(num_files)) 



    num_files = 0 

    for filename in os.listdir('/home/administrator/-share/New'): 
     succesfullRun = 0 
     num_files += 1 

     # print filename 
     logging.info('File: %s', filename) 

     filename = str(filename) 
     batchnumber = filename.strip("['SENSE-extract-ascii--.csv']")[:-9] 
     print batchnumber 

     newfullpath2Newfile = fullpath2Newfile + filename 
     fullpathfiles2Process = "/home/administrator/-share/SENSE-extract.csv" 
     fullpathProcessedfiles = "/home/administrator/-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='server', user='domain\user', password='pwd', tds_version='8.0') 
       cursor = conn.cursor() 
       cursor.execute("EXEC msdb.dbo.sp_start_job @job_name = 'jobname'") 

      except Exception as e: 
       (error_message) = e 
       #print(e) 

       ExceptionFlag = 1 

       print error_message 
       #print error_message 


       if "does not exist" in error_message: 
        # print "ERROR: Job name not found" 
        logging.info('ERROR: Job name not found') 

       if "job is already running" in error_message: 
        #print "ERROR: a Job is already running" 
        logging.info('ERROR: a Job is already running') 

       if "job already has a pending request" in error_message: 
        # print "ERROR: pending request" 
        logging.info('ERROR: pending request') 

       if "Unable to connect" in error_message: 
        # print "ERROR: Server unavailable" 
        logging.info('ERROR: Server unavailable') 

       time.sleep(0.5) 


      else: 
       # print "Job executed succesfully" 
       logging.info('Job %s Executed', batchnumber) 
       # Copy file to processed 
       shutil.copy(fullpathfiles2Process, fullpathProcessedfiles) 

       succesfullRun = 1 

      #conn.close() 
      time.sleep(0.5) 


logging.info('Finished uploading jobs') 
logging.info('END') 
exit() 

#END script 
0

문제는 귀하가 예외를 풀고 있다는 것입니다. 예외는 iterables로 간주되어서는 안되며, 인수를 포함한다고 가정해서는 안됩니다. 당신은 예외 클래스에 메시지를 전달하는 경우

In [2]: try: 
    ...:  assert False 
    ...: except Exception as e: 
    ...:  c, m = e 
    ...:  
--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-2-2e0dd0160822> in <module>() 
     2  assert False 
     3 except Exception as e: 
----> 4  c, m = e 
     5 

ValueError: need more than 0 values to unpack 

, 그것은 다음 1 개 값을 가지고 :

In [3]: try: 
    assert False, "This is a message" 
except Exception as e: 
    c, m = e 
    ...:  
--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-3-8af2ea044375> in <module>() 
     2  assert False, "This is a message" 
     3 except Exception as e: 
----> 4  c, m = e 
     5 

ValueError: need more than 1 value to unpack 

는 본질적으로이 오류 잡기 후 오류가 발생 오류 처리. 대신 역 추적 또는 Exception.args 사용하는 학습 :

역 추적 예

In [10]: try: 
    ....:   assert False, "This is a message" 
    ....: except Exception as error: 
    ....:  trace = traceback.format_exc() 
    ....:  print(trace, file=sys.stderr) 
    ....:  
Traceback (most recent call last): 
    File "<ipython-input-10-5a56c0f9112a>", line 2, in <module> 
    assert False, "This is a message" 
AssertionError: This is a message 

Exception.args 예를

In [11]: from __future__ import print_function 

In [12]: import sys 

In [13]: try: 
    ....:  assert False, "This is a message" 
    ....: except Exception as error: 
    ....:  print(error.args, file=sys.stderr) 
    ....:  
('This is a message',) 
+0

'(error_code, error_message) = e'가 생성되고 오류가 발생 했습니까? 이상한 것은 다른 서버에서 제대로 작동하고 있다는 것입니다 ... 추적 및 예외를 살펴보아야합니다 – maco1717

+0

다른 서버에서 여러 인수 @ maco1717이있을 수 있습니다. 또는 다른 서버에서 오류가 발생하지 않았습니다. 그것은 당신의 모든 문제를 해결하지는 못하지만, 당신이 묻는 질문을 해결합니다. 인수가 다르거 나 새 서버와 다른 예외 클래스를 발생 시키거나 이전 사례에서 오류가 발생하지 않아 블록이 실행되지 않습니다. 어느 쪽이든, 그 선은 나쁘다. –

+0

@ maco1717, 궁극적으로 코드를 완전히 디버깅 할 수는 없지만 전체 오류 추적 코드를 로깅하여 작업중인 내용을 알고있는 것이 좋습니다. 나는 그 코드 줄을 제거하는 것이 약간의 방법을 취할 것임을 알고있다. –