Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send email through smtp in superlance using crashmail

I'm trying to set up the email sending when a process changes state in supervisord by using crashmail. Having no luck with the default sendmail program which requires quite a lot of setup, I decided to go with a small script in Python that sends email using SMTP.

This worked very well (I received indeed an email saying that the process state changes) for the first state change but stop working afterward. I have tried to change different options in supervisord such as buffer_size or autorestart but it has no effect.

Here is the script I use to trigger the supervisord state changes:

import time

from datetime import datetime

if __name__ == '__main__':
    print(">>>>> STARTING ...", flush=True)
    while True:
        print("sleep now:", datetime.utcnow(), flush=True)
        time.sleep(30)
        raise Exception("meo meo")

This is the script that sends email through Gmail. This one will send the stdin.

#!/usr/bin/env python

import smtplib


def get_server():
    smtpserver = smtplib.SMTP('smtp.gmail.com:587')
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.login("[email protected]", "password")
    return smtpserver


if __name__ == '__main__':
    import sys

    data = sys.stdin.read()

    s = get_server()
    s.sendmail('[email protected]', ['[email protected]'], data)
    s.quit()

Here is my supervisord.conf

[eventlistener:crashmail]
command=crashmail -a -m [email protected] -s /home/ubuntu/mysendmail.py
events=PROCESS_STATE
buffer_size=102400
autorestart=true

Does anyone have any idea why? Thanks!

like image 453
Son Avatar asked Mar 01 '16 18:03

Son


1 Answers

I moved the eventlistener section to a separate file in /etc/supervisor/conf.d (instead of putting at the end of supervisord.conf) and now everything is working as expected ...

like image 76
Son Avatar answered Nov 15 '22 09:11

Son