This shows you the differences between two versions of the page.
restart_process_and_send_mail_if_fail [2012/04/10 18:29] k2patel created |
restart_process_and_send_mail_if_fail [2020/08/10 02:35] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Restart process ====== | ||
- | This script restart process and send email if it failes. | ||
- | |||
- | <code python> | ||
- | #!/usr/bin/env python | ||
- | # -*- coding: utf-8 -*- | ||
- | """ | ||
- | Created on Fri Apr 6 11:13:21 2012 | ||
- | |||
- | @author: kpatel | ||
- | """ | ||
- | |||
- | import subprocess | ||
- | import time | ||
- | import smtplib | ||
- | |||
- | # Define you email id to notify | ||
- | toaddrs = "kpatel@mcfapps.com" | ||
- | # Define Source Email ID | ||
- | fromaddr = "cron@mcfapps.com" | ||
- | |||
- | |||
- | def message(msg): | ||
- | server = smtplib.SMTP('localhost') | ||
- | subject = "Subject: [V6 CRON] Error : failed to execute" | ||
- | msg = subject + '\n' + msg | ||
- | print msg | ||
- | server.set_debuglevel(1) | ||
- | server.sendmail(fromaddr, toaddrs, msg) | ||
- | server.quit() | ||
- | |||
- | |||
- | try: | ||
- | args = ['/etc/init.d/postgresql', 'restart'] | ||
- | #subprocess.Popen(args, shell=True) | ||
- | subprocess.check_call(args) | ||
- | except subprocess.CalledProcessError as prit: | ||
- | message(str(prit)) | ||
- | |||
- | time.sleep(60) | ||
- | |||
- | try: | ||
- | subprocess.check_call(['su', '-', 'mcfapps', '-c', 'live/interchange/bin/interchange']) | ||
- | except subprocess.CalledProcessError as prit: | ||
- | message(str(prit)) | ||
- | </code> |