如何在Python中执行一次脚本

如何在Python中执行一次脚本,python,monitoring,Python,Monitoring,我有以下Python代码,我只想执行ServiceRestart一次 # Logic to read the result file and send email if self.flag: fn = open(result_file_name, 'r+') result_data = fn.read() self.logger.info('Calling send email method') self.l

我有以下Python代码,我只想执行ServiceRestart一次

        # Logic to read the result file and send email
    if self.flag:
        fn = open(result_file_name, 'r+')
        result_data = fn.read()
        self.logger.info('Calling send email method')
        self.logger.info(result_data)
        if(((day == 'Saturday') and (hr >= '06' and hr <'12')) or (day == 
        'Sunday')):
            print('Mail will not go')
        else:                
            self.email_util.send_email(result_data)
            serviceName = "raha-connector"
            win32serviceutil.RestartService(serviceName) //this i want to execute once
            result_data = 'Connector started successfully'
            self.email_util.send_email(result_data)
            self.logger.info('Match found, hence send email method called')
            self.logger.info('End of search_file method : file_name -%s, 
            criteria - %s, last scanned row - %s',
            file_name, criteria, last_scanned_row)
目前的情况是,我已经配置了四个错误,我下面的脚本将从JSON文件中获取这些错误并发送电子邮件警报,但由于我在else下添加了服务重启,如果同时发现这四个错误,它将重启服务4次

我只想运行serviceRestart一次

        # Logic to read the result file and send email
    if self.flag:
        fn = open(result_file_name, 'r+')
        result_data = fn.read()
        self.logger.info('Calling send email method')
        self.logger.info(result_data)
        if(((day == 'Saturday') and (hr >= '06' and hr <'12')) or (day == 
        'Sunday')):
            print('Mail will not go')
        else:                
            self.email_util.send_email(result_data)
            serviceName = "raha-connector"
            win32serviceutil.RestartService(serviceName) //this i want to execute once
            result_data = 'Connector started successfully'
            self.email_util.send_email(result_data)
            self.logger.info('Match found, hence send email method called')
            self.logger.info('End of search_file method : file_name -%s, 
            criteria - %s, last scanned row - %s',
            file_name, criteria, last_scanned_row)
#读取结果文件并发送电子邮件的逻辑
如果self.flag:
fn=打开(结果文件名“r+”)
结果_data=fn.read()
self.logger.info('调用发送电子邮件方法')
self.logger.info(结果数据)

如果(((day=='Saturday')和(hr>='06'和hr使用布尔值)

has_restarted = False
 # Logic to read the result file and send email
    if self.flag:
        fn = open(result_file_name, 'r+')
        result_data = fn.read()
        self.logger.info('Calling send email method')
        self.logger.info(result_data)
        if(((day == 'Saturday') and (hr >= '06' and hr <'12')) or (day == 
        'Sunday')):
            print('Mail will not go')
        else:                
            self.email_util.send_email(result_data)
            serviceName = "raha-connector"
            if not has_restarted:
                win32serviceutil.RestartService(serviceName) //this i want to execute once
                has_restarted = True
            result_data = 'Connector started successfully'
            self.email_util.send_email(result_data)
            self.logger.info('Match found, hence send email method called')
            self.logger.info('End of search_file method : file_name -%s, 
            criteria - %s, last scanned row - %s',
            file_name, criteria, last_scanned_row)
已重新启动=错误
#读取结果文件并发送电子邮件的逻辑
如果self.flag:
fn=打开(结果文件名“r+”)
结果_data=fn.read()
self.logger.info('调用发送电子邮件方法')
self.logger.info(结果数据)
如果(((天=='周六')和(小时>='06'和小时