2016-08-08 60 views
0

我编写mail.py(使用webpy)向我发送每台机器的IP地址。在脚本中成功但在rc.local中失败

#!/usr/bin/env python 
#coding=utf-8 

import web 
def send_mail(send_to, subject, body, cc=None, bcc=None): 
    try: 
     web.config.smtp_server = 'xxxxx' 
     web.config.smtp_port = 25  
     web.config.smtp_username = 'xxx' 
     web.config.smtp_password = 'xxx' 
     web.config.smtp_starttls = True 
     send_from = 'xxx'  

     web.sendmail(send_from, send_to, subject, body, cc=cc, bcc=bcc) 
     return 1 #pass 
    except Exception, e: 
     print e 
     return -1 #fail 
if __name__=='__main__': 
    print "in mail.py" 
    f=file('/home/spark/Desktop/ip.log') 
    f1=f.read() 
    f.close() 
    send_to = ['xxxx']   
    subject = 'xxxx' 
    body = 'ip:',f1 
    send_mail(send_to, subject, body) 

rc.local中

bash deploy.sh & 
exit 0 

deploy.sh

#!/usr/bin/env 
cd /home/spark/Desktop 
python mail.py >>deploy.log 
echo "-----------------------------------------------------------" 

,如果我做“蟒蛇mail.py'.But我可以接收电子邮件,当我把它放在rc.local中,我无法收到电子邮件,部署日志输出中的消息 [Errno -2]名称或服务未知。

我对这个输出感到困惑。

+0

我改变了web.config.smtp_server与IP地址,而不是域名,它的工作。我认为这个错误是因为一些配置和服务在rc.local过程中不可用。有用的链接是[sendmail](https://wiki.list.org/DOC/Mail%20delivery%20fails%20with%20%22(-2,%20'Name%20or%20service%20not%20known')%22 ,%20aka%20I%20am%20not%20receiving%20any%20notification%20messages%20from%20Mail) [http://support.microsoft.com/kb/23777121/why-am-i-getting-socket- gaierror-errno-2-from-python-httplib) – makeapp

+0

[技巧来调试rc.local](http://stackoverflow.com/questions/7783341/run-script-with-rc-​​local-script-works-but-未在启动) – makeapp

回答

1

发生这种情况的原因可能是PATH在运行rc.local时不同。具体来说,web.sendmail可能会在路径中找到sendmail,但它尚未出现。请参阅文档here

路径可能特定于您的系统。要进行调试,您可以将rc.local中的内容转储到/tmp/rc.local.log等文件中,并在系统启动时检查它:例如env >>/tmp/rc.local.log

请注意,如果您在启动过程中安装了多个驱动器,则此时可能尚未安装包含sendmail的驱动器。这是一个需要处理的痛苦。要仔细检查,请添加mount >>/tmp/rc.local.log

相关问题