Exemple #1
0
def _stop():
    print(u'======stop========')
    # 在任务表删除定时计划
    operate_crontab("del")

    # 查询进程组id
    gpid = _status()
    if gpid:
        # 进程组存在,杀死进程
        import signal
        # 杀死进程组
        os.killpg(int(gpid), signal.SIGKILL)
        i = 0
        while _status():
            time.sleep(1)
            i += 1
            print(u'等待{}秒'.format(i))

        print(u'{}成功停止{}服务[gpid={}]'.format(datetime.now(), base_path, gpid))

        try:
            sendmail.sendmail(
                subject='Notification: {}目录下服务进程停止'.format(base_path),
                msgcontent='服务进程[gpid={}]停止'.format(gpid))
        except:
            print(u'发送通知邮件失败')
            pass
    else:
        print(u'{}服务进程没有运行'.format(base_path))
Exemple #2
0
def _stop():
    print(u'======stop========')
    # 在任务表删除定时计划
    operate_crontab("del")

    # 查询进程组id
    gpid = _status()
    if gpid:
        # 进程组存在,杀死进程
        import signal
        # 杀死进程组
        if USE_GPID:
            # 杀死进程组
            os.killpg(int(gpid), signal.SIGKILL)
        else:
            os.kill(int(gpid), signal.SIGKILL)
        i = 0
        while _status():
            time.sleep(1)
            i += 1
            print(u'等待{}秒'.format(i))

        print(u'{}成功停止{}服务[gpid={}]'.format(datetime.now(), base_path,gpid))

        try:
            sendmail.sendmail(subject='Notification: {}目录下服务进程停止'.format(base_path),
                              msgcontent= '服务进程[gpid={}]停止'.format(gpid))
        except:
            print(u'发送通知邮件失败')
            pass
    else:
        print(u'{}服务进程没有运行'.format(base_path))
Exemple #3
0
def _start():
    """
    启动服务
    :return: 
    """
    # 获取进程组id
    gpid = _status()

    if _check_stop_time():
        # 属于停止运行期间
        if gpid:
            print(u'现在属于停止运行时间,进程组ID存在,将杀死服务进程:[gpid={}]'.format(gpid))
            import signal
            # 杀死进程组
            os.killpg(int(gpid), signal.SIGKILL)
            while _status():
                time.sleep(1)
            print('进程组已停止运行[gpid={}]'.format(gpid))
            try:
                sendmail.sendmail(
                    subject='Notification: {0}目录下服务 killed by service.py'.
                    format(base_path),
                    msgcontent='停止运行时间,已将进程组[gpid={}]杀死.'.format(gpid))
            except:
                print(u'发送通知邮件失败', file=sys.stderr)
                pass
        else:
            print(u'现在属于停止运行时间,不启动服务')
    else:
        # 属于运行时间
        if not gpid:
            print(u'属于运行时间,将启动服务')
            os.popen(program_command)
            while True:
                gpid = _status()
                if gpid:
                    print('属于运行时间,成功启动服务[gpid={}]'.format(gpid))
                    try:
                        sendmail.sendmail(
                            subject='Notification: {0}目录下进程启动'.format(
                                base_path),
                            msgcontent='属于运行时间,已启动服务[gpid={}]'.format(gpid))
                    except:
                        print(u'发送通知邮件失败', file=sys.stderr)
                        pass
                    break
                time.sleep(1)
        else:
            print(u'属于运行时间,服务已运行')
Exemple #4
0
def _start():
    """
    启动服务
    :return: 
    """
    # 获取进程组id
    gpid = _status()
    if _check_stop_time():
        # 属于停止运行期间
        if gpid:
            print(u'现在属于停止运行时间,进程组ID存在,将杀死服务进程:[gpid={}]'.format(gpid))
            import signal
            # 杀死进程组
            os.killpg(int(gpid), signal.SIGKILL)
            i = 0
            while _status():
                time.sleep(1)
                i += 1
                print(u'杀死进程中,等待{}秒'.format(i))

            print('进程组已停止运行[gpid={}]'.format(gpid))
            try:
                sendmail.sendmail(
                    subject='Notification: {0}目录下服务 killed by service.py'.
                    format(base_path),
                    msgcontent='停止运行时间,已将进程组[gpid={}]杀死.'.format(gpid))
            except:
                print(u'发送通知邮件失败', file=sys.stderr)
                pass
        else:
            print(u'现在属于停止运行时间,不启动服务')
    else:
        # 属于运行时间
        if not gpid:
            print(u'属于运行时间,将启动服务:{}'.format(program_command))
            if os.path.isfile(gpid_file):
                print(u'{0}文件存在,先执行删除'.format(gpid_file))
            try:
                os.remove(gpid_file)
            except:
                pass

            os.popen(program_command)
            i = 0
            while True:
                gpid = _status()
                if gpid:
                    print('{}属于运行时间,成功启动服务[gpid={}]'.format(
                        datetime.now(), gpid))
                    try:
                        sendmail.sendmail(
                            subject='Notification: {0}目录下进程启动'.format(
                                base_path),
                            msgcontent='{}属于运行时间,已启动服务[gpid={}]'.format(
                                datetime.now(), gpid))
                    except:
                        print(u'发送通知邮件失败', file=sys.stderr)
                        pass
                    break

                i += 1
                print(u'启动进程中,等待{}秒'.format(i))
                time.sleep(1)

        else:
            print(u'属于运行时间,{}服务已运行'.format(base_path))
Exemple #5
0
def _start():
    """
    启动服务
    :return: 
    """
    # 获取进程组id
    gpid = _status()
    if _check_stop_time():
        # 属于停止运行期间
        if gpid:
            print(u'现在属于停止运行时间,进程组ID存在,将杀死服务进程:[gpid={}]'.format(gpid))
            import signal
            if USE_GPID:
                # 杀死进程组
                os.killpg(int(gpid), signal.SIGKILL)
            else:
                os.kill(int(gpid), signal.SIGKILL)
            i = 0
            while _status():
                time.sleep(1)
                i += 1
                print(u'杀死进程中,等待{}秒'.format(i))
                if i > 30:
                    print(u'杀死进程失败,退出')
                    exit(1)

            print('进程组已停止运行[gpid={}]'.format(gpid))
            try:
                sendmail.sendmail(subject='Notification: {0}目录下服务 killed by service.py'.format(base_path),
                                  msgcontent='停止运行时间,已将进程组[gpid={}]杀死.'.format(gpid))
            except:
                print(u'发送通知邮件失败', file=sys.stderr)
                pass
        else:
            print(u'{} 现在属于停止运行时间,不启动服务'.format(datetime.now()))
    else:
        # 属于运行时间
        if not gpid:
            print(u'{}属于运行时间,将启动服务:{}'.format(datetime.now(),program_command))
            if os.path.isfile(gpid_file):
                print(u'{0}文件存在,先执行删除'.format(gpid_file))
            try:
                os.remove(gpid_file)
            except:
                pass

            os.popen(program_command)
            i = 0
            while True:
                gpid = _status()
                if gpid:
                    print('{}属于运行时间,成功启动服务[gpid={}]'.format(datetime.now(), gpid))
                    try:
                        sendmail.sendmail(
                            subject='Notification: {0}目录下进程启动'.format(base_path),
                            msgcontent='{}属于运行时间,已启动服务[gpid={}]'.format(datetime.now(), gpid))
                    except:
                        print(u'发送通知邮件失败', file=sys.stderr)
                        pass
                    break

                i += 1
                print(u'启动进程中,等待{}秒'.format(i))
                if i > 30:
                    print(u'启动进程失败,退出')
                    exit(1)
                time.sleep(1)


        else:
            print(u'{}属于运行时间,{}服务已运行'.format(datetime.now(),base_path))