简体   繁体   English

调度python脚本时出错:fcron [12036]:无法执行'/ usr / bin / sendmail':没有这样的文件或目录

[英]Error when scheduling python script: fcron[12036]: Couldn't exec '/usr/bin/sendmail': No such file or directory

I have a python script that I would like to schedule with fcrontab. 我有一个python脚本,我想用fcrontab安排。 This script sends emails based on the status field set in one of the columns. 此脚本根据其中一列中设置的状态字段发送电子邮件。 The script executes in the expected manner when I invoke the following: 当我调用以下内容时,脚本以预期的方式执行:

python2 mail.py

This results in the following and expected output: 这导致以下和预期的输出:

Successfully sent email... 
Update sent status on EdPost...  xxxxxx
Update sent status on EdPost...  xxxxxx
Update sent status on EdPost...  xxxxxx
Update sent status on EdPost...  xxxxxx
Update sent status on EdPost...  xxxxxx
Update sent status on EdPost...  xxxxxx
Update sent status on EdPost...  xxxxxx
Update sent status on EdPost...  xxxxxx

However when I try to run this script as a cron job, I end up with the mentioned error. 但是,当我尝试将此脚本作为cron作业运行时,我最终得到了上述错误。

Cron: 克龙:

0 15 * * * /home/x/JobParse/mail.py >> /home/x/JobParse/mailer.log

Error: 错误:

fcron[23191]: Couldn't exec '/usr/bin/sendmail': No such file or directory

I have another python script scheduled in a similar fashion that runs just fine with the #!/usr/bin/env python2 shebang. 我有另一个python脚本以类似的方式安排,与#!/usr/bin/env python2 shebang运行良好。 That script does not use the smtplib library though. 该脚本不使用smtplib库。

The script I'm trying to schedule is below: 我正在尝试安排的脚本如下:

mail.py mail.py

#!/usr/bin/python2
import smtplib
from db import get_records,update_sent_status
import traceback
import datetime
configs = [line.rstrip('\n') for line in open('configs')]
usr = configs[0]
pwd = configs[1]
rcvr = configs[2]
receivers = [rcvr]
date = datetime.datetime.now().date()
posts = []
edpost_tbl_rows= ""
appli_tbl_rows= ""

ed_posts = get_records(0) # * non-appli and  unsent
ed_posts_ids = [row[0] for row in ed_posts]
ed_posts_sent = get_records(1) # post_id

uniq_appli = get_records(2) # unique unsent appli
uniq_appli_ids = [row[0] for row in uniq_appli]

all_appli_sent = [row[0] for row in get_records(4)] # all app_id sent
all_appli_unsent = [row[0] for row in get_records(5)] # all app_id unsent


# No jobs to send so exit
if len(all_appli_unsent) == 0 and len(ed_posts) == 0:
    print "Exit: No new unique posts.."
    exit()

# Transform db data into html table row format
for row in ed_posts:
    edpost_tbl_rows += \
            "<tr style=\"border:1px solid black\"><td style=\"border:1px solid black\">"\
            +str(row[0])\
            +"</td><td style=\"border:1px solid black\"><a href="\
            +'"'\
            +row[1]\
            +'"'\
            +">"\
            +row[4]\
            +"</a></td><td style=\"border:1px solid black\">"\
            +row[5]\
            +"</td><td style=\"border:1px solid black\">"\
            +row[6]\
            +"</td></tr>"

# Transform applitrack posts into html table
for row in uniq_appli:
    # check if sent before - app_id not unique on edpost
    if row[3] not in all_appli_sent:
        print "New unique & unsent applitrack post... ",row[3]
        appli_tbl_rows += \
            "<tr style=\"border:1px solid black\"><td style=\"border:1px solid black\">"\
            +str(row[0])\
            +"</td><td style=\"border:1px solid black\"><a href="\
            +'"'\
            +row[2]\
            +'"'\
            +">"\
            +str(row[3])\
            +"</a></td><td style=\"border:1px solid black\"><a href="\
            +'"'\
            +row[1]\
            +'"'\
            +">"\
            +row[4]\
            +"</a></td><td style=\"border:1px solid black\">"\
            +row[5]\
            +"</td><td style=\"border:1px solid black\">"\
            +row[6]\
            +"</td></tr>"
    else:
        print "Skipping applitrack post... ",row[3]


message = """From: JobUpdates <{usr}>
To: xxxx <{rcvr}>
MIME-Version: 1.0
Content-type: text/html; charset=utf-8
Subject: New Job Postings: {date}

<p>Check out these postings you may have not have seen yet :)</p>
<p>Searched: words

<h2>Jobs With Applitrack</h2>
<table style="width:100%;border:1px solid black">
    <tr style="border:1px solid black">
        <th style="border:1px solid black">EdPost ID</th>
        <th style="border:1px solid black">AppliTrack ID</th>
        <th style="border:1px solid black">Description</th>
        <th style="border:1px solid black">Post DT</th>
        <th style="border:1px solid black">Exp DT</th>
    </tr>
    {appli_tbl_rows}
</table>
</br>
</br>
<h2>Jobs Without Applitrack</h2>
<table style="width:100%;border:1px solid black">
    <tr style="border:1px solid black">
        <th style="border:1px solid black">EdPost ID</th>
        <th style="border:1px solid black">Description</th>
        <th style="border:1px solid black">Post DT</th>
        <th style="border:1px solid black">Exp DT</th>
    </tr>
    {edpost_tbl_rows}
</table>
""".format(usr=usr,rcvr=rcvr,date=date,edpost_tbl_rows=edpost_tbl_rows,appli_tbl_rows=appli_tbl_rows)

try:
    smtpObj = smtplib.SMTP('localhost',1025)
    smtpObj.login(usr,pwd)
    smtpObj.sendmail(usr, rcvr, message)
    print "Successfully sent email... "
    for post_id in ed_posts_ids:
        print "Update sent status on EdPost... ",post_id
        update_sent_status(post_id)

    for app_id in all_appli_unsent:
        print "Update sent status on Appli... ",app_id
        update_sent_status(app_id,1)

except smtplib.SMTPException:
    print "ERROR: unable to send email... "
    print traceback.format_exc()

I've tried this with the #!/usr/bin/env python2 shebang as well to no avail. 我用#!/usr/bin/env python2 shebang试过这个也无济于事。

I was able to resolve the issue without installing sendmail. 我能够在不安装sendmail的情况下解决问题。 This was done by moving the Python path into the script at the start of it as follows: 这是通过将Python路径移动到脚本开头来完成的,如下所示:

import os
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)

See Pitto's answer here . 见Pitto的答案在这里

I'm guessing updating the path variable for cron's environment from crontab itself would be a viable solution as well. 我猜测从crontab本身更新cron环境的路径变量也是一个可行的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 /usr/bin/env: python3: 从 cron 运行脚本时没有这样的文件或目录 - /usr/bin/env: python3: No such file or directory when running script from cron Xcode swift iOS 尝试构建项目时出现“usr/bin/python 没有这样的文件或目录”错误 - Xcode swift iOS project gives "usr/bin/python no such file or directory" error when try to build project / usr / bin / env:python2.6:没有这样的文件或目录错误 - /usr/bin/env: python2.6: No such file or directory error 获取usr / bin / python2.7:那里没有这样的文件或目录 - Getting usr/bin/python2.7: No such file or directory when it's there /usr/bin/env: python,没有那个文件或目录 - /usr/bin/env: python, No such file or directory hadoop /usr/bin/env: python: 没有那个文件或目录 - hadoop /usr/bin/env: python: No such file or directory WSL: /usr/bin/env: 'python': 没有那个文件或目录 - WSL: /usr/bin/env: ‘python’: No such file or directory 错误的解释器没有这样的文件或目录 /usr/bin/python - bad interpreter no such file or directory /usr/bin/python ubuntu /usr/bin/env: python: 没有那个文件或目录 - ubuntu /usr/bin/env: python: No such file or directory python:无法打开文件&#39;C:\\usr\\local\\bin\\django-admin.py&#39;:[Errno 2]没有这样的文件或目录 - python: can't open file 'C:\\usr\\local\\bin\\django-admin.py': [Errno 2] No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM