簡體   English   中英

Python 腳本未從 Cron 執行

[英]Python script not executed from Cron

我正在嘗試從 Cron 運行 Python 腳本,但它不起作用。 我已經嘗試了我在多個 Stackoverflow 問題中看到的所有內容。這台機器是運行 Raspbian 的 Raspberry。 以下代碼是crontab的版本:

PATH=/usr/sbin:/usr/bin:/sbin/bin:/sbin:/bin:/home/pi/miniconda/bin:/usr/local/bin:/usr/local/sbin

*/5 * * * * rsync -az --timeout=10 --progress pritms@bigdata.trainhealthmanagement.com:Upload/*.csv /home/pi/PAD-S100/PAD-S100-Bloque_Motor/from_repo/ | /bin/sh /home/pi/PAD-S100/PAD-S100-Bloque_Motor/adddate_to_logs.sh >> /home/pi/PAD-S100/PAD-S100-Bloque_Motor/log.log 2>&1
*/5 * * * * /bin/sh /home/pi/PAD-S100/PAD-S100-Bloque_Motor/from_repo/launcher.sh | /bin/sh /home/pi/PAD-S100/PAD-S100-Bloque_Motor/adddate_to_logs.sh >> /home/pi/PAD-S100/PAD-S100-Bloque_Motor/log.log 2>&1
*/30 * * * * rm /home/pi/PAD-S100/PAD-S100-Bloque_Motor/log.log
* * * * * /usr/bin/python /home/pi/PAD-S100/PAD-S100-Bloque_Motor/from_repo/event_management.py | /bin/sh /home/pi/PAD-S100/PAD-S100-Bloque_Motor/adddate_to_logs.sh >> home/pi/PAD-S100/PAD-S100-Bloque_Motor/log.log 2>&1
0 0 * * * rm /home/pi/PAD-S100/PAD-S100-Bloque_Motor/from_repo/*.csv | /bin/sh /home/pi/PAD-S100/PAD-S100-Bloque_Motor/adddate_to_logs.sh >> /home/pi/PAD-S100/PAD-S100-Bloque_Motor/log.log 2>&1

Crontab觀察:

  • 包括從echo $PATH獲得的路徑。
  • launcher.shaddddate_to_logs.shevent_management是使用命令sudo chmod a+x <file_name>的可執行文件。

log.log文件沒有顯示任何奇怪的東西。 系統日志文件/var/log/syslog有以下日志:

Feb 27 15:11:08 raspberrypi cron[21814]: sendmail: Cannot open :25
Feb 27 15:12:01 raspberrypi rsyslogd-2007: action 'action 17' suspended, next retry is Mon Feb 27 15:13:31 2017 [try http://www.rsyslog.com/e/2007 ]
Feb 27 15:12:01 raspberrypi CRON[22209]: (pi) CMD (/usr/bin/python /home/pi/PAD-S100/PAD-S100-Bloque_Motor/from_repo/event_management.py | /bin/sh /home/pi/PAD-S100/PAD-S100-Bloque_Motor/adddate_to_logs.sh >> home/pi/PAD-S100/PAD-S100-Bloque_Motor/log.log 2>&1)
Feb 27 15:12:09 raspberrypi sSMTP[22212]: Unable to set UsesSTARTTILS=""
Feb 27 15:12:09 raspberrypi sSMTP[22212]: Unable to locate
Feb 27 15:12:09 raspberrypi cron[21814]: sendmail: Cannot open :25
Feb 27 15:12:09 raspberrypi sSMTP[22212]: Cannot open :25
Feb 27 15:12:09 raspberrypi CRON[22205]: (pi) MAIL (mailed 178 bytes of output but got status 0x0001 from MTA#012)

我們可以觀察到,失敗的 crontab 行很可能是 python 腳本之一。 由於我不是 Linux 專家,我相信這可能與sSMTP有關。 每次調用 cron python 腳本后都會出現相同類型的錯誤日志。 但我不知道如何修復它或配置本地電子郵件。

這是event_management.py文件的一段代碼:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import imaplib
import email
import csv
import datetime


EMAIL = <email_user>
FROM_PWD = <password>
SMTP_SERVER = 'mail.o365.alstom.com'
datum = dict()
translate = {'#09': 1, '#0A': 2, '#0B': 3, '#0C': 4}


def connect_imap():
    mail = imaplib.IMAP4_SSL(SMTP_SERVER)
    mail.login(EMAIL, FROM_PWD)
    return mail


def read_email_from_gmail(writer, mail):
    mail.select('BRMS')

    kind, data = mail.search(None, 'ALL')
    mail_ids = data[0]
    id_list = mail_ids.split()
    first_email_id = int(id_list[0])
    latest_email_id = int(id_list[-1])

    for i in range(latest_email_id, first_email_id, -1):
        typ, data = mail.fetch(i, '(RFC822)')

        for response_part in data:
            if isinstance(response_part, tuple):
                msg = email.message_from_string(response_part[1])
                for part in msg.walk():
                    if part.get_content_type() == 'text/html':
                        content = part.get_payload()
                        manage_email_content(content, writer)

    return 0


def manage_email_content(content, writer):
    content = content.split('\n')
    for i, line in enumerate(content):
        if 'Alert description' in line:
            line = line.split()
            datum['Event code'] = line[-1][4:]
            if line[-1][:3] in translate:
                datum['Motor block num'] = translate[line[-1][:3]]
            else:
                datum['Motor block num'] = 'Defecto ajeno al bloque motor'
        elif 'Alert condition' in line:
            line = line.split()
            datum['Code description'] = ' '.join(line[4:])
        elif 'Unit id' in line:
            line = line.split()
            datum['Train num'] = line[3][3:]
        elif 'Alert raised' in line:
            line = line.split()
            datum['Date'] = line[4][:10]
            datum['Time'] = line[4][11:]
    writer.writerow(datum)
    print datum
    return 0


def move_to_trash_before_date(mail, folder, days_before):
    # required to perform search, m.list() for all lables, '[Gmail]/Sent Mail'
    no_of_msgs = int(mail.select(folder)[1][0])
    print("- Found a total of {1} messages in '{0}'.".format(folder, no_of_msgs))

    before_date = (datetime.date.today() - datetime.timedelta(days_before)).strftime("%d-%b-%Y")
    typ, data = mail.search(None, '(BEFORE {0})'.format(before_date))  # search pointer for msgs before before_date

    if data != ['']:  # if not empty list means messages exist
        no_msgs_del = data[0].split()[-1]  # last msg id in the list
        print("- Marked {0} messages for removal with dates before {1} in '{2}'.".format(no_msgs_del, before_date, folder))
        mail.store("1:{0}".format(no_msgs_del), '+X-GM-LABELS', '\\Trash')  # move to trash
        empty_folder(mail, 'Elementos eliminados', do_expunge=True)  # can send do_expunge=False, default True
    else:
        print("- Nothing to remove.")

    return 0


def empty_folder(mail, folder, do_expunge=True):
    mail.select(folder)  # select all trash
    mail.store("1:*", '+FLAGS', '\\Deleted')  # Flag all Trash as Deleted
    if do_expunge:  # See Gmail Settings -> Forwarding and POP/IMAP -> Auto-Expunge
        mail.expunge()  # not need if auto-expunge enabled
    else:
        print("Expunge was skipped.")
    return 0


def disconnect_imap(mail):
    mail.close()
    mail.logout()
    return 0


def main():
    with open('email_data.csv', 'w') as f:
        writer = csv.DictWriter(f, fieldnames=['Time', 'Date', 'Train num', 'Motor block num',
                                               'Event code', 'Code description'], delimiter=';')
        try:
            m = connect_imap()
            writer.writeheader()
            read_email_from_gmail(writer, m)
            move_to_trash_before_date(m, 'BRMS', 15)  # inbox cleanup, before 15 days
            disconnect_imap(m)

        except Exception, e:
            print str(e)

if __name__ == "__main__":
    main()

event_management文件連接到 Outlook 電子郵件文件夾,讀取電子郵件並使用從電子郵件內容中提取的數據構建一個 CSV 文件。 該文件工作正常,已經過測試; 並且在手動執行時工作正常(不使用 Cron)。 所以我不確定它與系統日志中出現的sSMTP問題有關。

我會感謝每一種幫助或建議!

經過一些測試和閱讀其他用戶的答案,我發現了問題所在。 它是兩個不直接相關的不同問題的組合,但一起使這個問題成為調試的痛苦。


第一個問題:

log.log文件包含來自三個不同可執行文件的日志和錯誤,因此我沒有注意到evet_management文件沒有正確的權限。 我沒有很好地應用chmod命令,我沒有注意到它,因為它包含很多數據。

結論 1:一個cronjob ,一個log文件。

結論 2: /var/log/syslog包含大量數據,來自各種資源,因此在嘗試調試時可能會使您感到困惑。 最好分開生成日志文件。


第二個問題:

我的機器上安裝了兩個 Python 發行版。 當我手動執行腳本時,使用了一個。 當 Cron 執行腳本時,會使用另一個腳本。 此外,當第一個問題得到解決時,我注意到了它。 log文件中通過 Cron 運行 Python 腳本時出現模塊未找到的錯誤,但在手動執行時完美運行。 因此,我看到當使用pip install <module-name>時,它只適用於一個發行版。 要檢查我使用的 Python 版本:

which Python

結論:要聰明,不要像我一樣,不要亂用多個 Python 發行版。


獎勵:始終使用完整路徑來清晰。 Cronenv與你的不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM