User Tools

Site Tools


restart_process_and_send_mail_if_fail

Restart process

This script restart process and send email if it failes.

#!/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 = "[email protected]"
# Define Source Email ID
fromaddr = "[email protected]"
 
 
def message(msg):
    server = smtplib.SMTP('localhost')
    subject = "Subject: [CRON] Error : failed to execute Process"
    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.check_call(args)
except subprocess.CalledProcessError as prit:
    message(str(prit))
 
time.sleep(60)
 
try:
    subprocess.check_call(['su', '-', 'kpatel', '-c', 'live/interchange/bin/interchange'])
except subprocess.CalledProcessError as prit:
    message(str(prit))
restart_process_and_send_mail_if_fail.txt · Last modified: 2012/04/10 14:30 by k2patel