python3 实现应用启动及关键字检测

我们在应用的时候,经常需要实现应用启动及检测功能。我们启动命令
python3 restart_and_watchlog.py “sh bin/start.sh” “logs/log.log” “Started Application in” 20
第一个restart_and_watchlog.py 检测脚本
sh bin/start.sh 应用启动命令
logs/log.log 应用启动日志
Started Application in 关键字
20 过期时间20s

# -*- coding: utf-8 -*-#

#-------------------------------------------------------------------------------
# Name:         restart_and_watchlog.py
# Description:
# Author:       Administrator
# Date:         2022/3/13
#-------------------------------------------------------------------------------

import sys
import importlib
import subprocess
import signal
import os
from collections import deque

importlib.reload(sys)

# python3 restart_and_watchlog.py "sh bin/start.sh" "logs/log.log" "Started Application in" 20
#'sh app.sh restart'
start_shell=sys.argv[1]
#'servicelog'
log_file_name=sys.argv[2]
#'Started DemoApplication in'
keywork_success =sys.argv[3]
time_out: int=sys.argv[4]

def myHandler(signum,frame):
    raise Exception("TimeoutError,no found keyword of success.")


logline_queue=deque(maxlen=30)


def log_search(logfile,search_content):
    print("#"*50)
    #防止读取到老日志
    shell_string='tail -n 10 -F '+logfile
    print('shell_string:'+shell_string)
    working_file=subprocess.Popen(['tail','-n','10','-F',logfile],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    print("execute end")
    try:
        while True:
            line=str(working_file.stdout.readline())
            # process data
            logline_queue.append(line)
            if len(line)>0:
                if search_content in line:
                    print("success keyword of success:"+line)
                    return True
            else:
                return False
    finally:
        os.kill(working_file.pid,signal.SIGKILL)


def log_search_timeout(logfile,seach_content,time_out=10):
    try:
        signal.signal(signal.SIGALRM,myHandler)
        signal.alarm(int(time_out))
        result=log_search(logfile,seach_content)
        signal.alarm(0)
    except Exception as e:
        print("execute log search exception",e)
        return False
    return True

def execute_shell(shellstring):
    print("#"*50)
    print("execute start shell:"+shellstring)
    write_file=subprocess.Popen(shellstring,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    print(write_file.stdout.read())
    print("#"* 50)


execute_shell(start_shell)
#检查脚本执行日志,可以用阻塞式脚本,获取到期望的字符串,就退出

result=log_search_timeout(logfile=log_file_name,seach_content=keywork_success,time_out=time_out)

print("execute shell log is:"+str(result))

if result:
    exit(0)

else:
    if len(logline_queue)>0:
        print("error log content:")
        for logline in logline_queue:
            print(logline)
        pass

    exit(-1)



验证结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值