Python-watchdog

一个canal的watchdog脚本 check-canal.py

import psutil
import os
import time

while True:
    count = 0
    pre_run = list()
    for pid in psutil.pids():
        p = psutil.Process(pid)
        for z in p.cmdline():
            if "Canal" in z:
                if p.is_running():
                    count += 1
                    pre_run.append(p)
    if count < 3:
        os.system('''/var/canal/deployer/bin/stop.sh''')
        os.system('''/var/canal/console/bin/stop.sh''')
        os.system('''/var/canal/adapter/bin/stop.sh''')

        current = count
        while current > 0:
            time.sleep(1)
            total = 0
            for p in pre_run:
                if p.is_running():
                    print(z + ' is still running.')
                else:
                    total += 1
            current = count-total
        os.system('''/var/canal/console/bin/startup.sh''')
        time.sleep(8)
        os.system('''/var/canal/deployer/bin/startup.sh local''')
        time.sleep(2)
        os.system('''/var/canal/adapter/bin/startup.sh''')
    time.sleep(3)

监控脚本的watchdog,canal-watchdog

import psutil
import os
import time

count = 0
for pid in psutil.pids():
    p = psutil.Process(pid)
    for z in p.cmdline():
        if "check-canal" in z:
            if p.is_running():
                print(z + ' is running.')
                count += 1
if count == 0 :
    os.system('''python3 /root/check-canal.py''')

使用Linux定时任务,最低是1分钟一次,这也是为啥要用脚本监控脚本

crontab -e
MAILTO=""
*/1 * * * * python3 /root/canal-watchdog.py

参考连接
watchdog用于监测系统服务是否挂掉,挂掉则自动重启

posted @ 2022-02-21 14:50  kylinmac  阅读(269)  评论(0)    收藏  举报