应用程序监听器

简单的应用程序监听程序,若被监听的程序崩溃会自动重启,类似Unix/Linux命令screen

目的:为该死的Windows而作

运行环境:Python

source code:

#!/usr/bin/python

import psutil
import os
import threading
import time

# you case applictions
processs = {
    "game_server_d.exe": "D:\\ghoul_app\\game_server\\",
}
# watch interval(s)
interval = 2


def recordLog(log):
    filename = "watch.log"
    with open(filename, "a") as fp:
        print(log)
        fp.write(log)
        fp.write("\n")


def isRunning(path, process_name):
    try:
    	pids = psutil.pids()
        for pid in pids:
            p = psutil.Process(pid)
            # print("pid-%d, pname-%s" % (pid, p.name()))
            try:
                if p.name() == process_name and p.cwd() == path:
                    return True
            except psutil.AccessDenied as id:
                continue
    except psutil.NoSuchProcess as identifier:
        print(identifier.msg)
        return False
    return False


def restart(path):
    if len(path) > 0:
        os.system(path)


def onTick():
    for k, path in processs.iteritems():
        if not isRunning(path, k):
            fullPath = path + k
            log = str("[%s] restart %s" % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), fullPath))
            recordLog(log)
            old_path = os.getcwd()
            os.chdir(path)
            restart(fullPath)
            os.chdir(old_path)
    # repeat
    global timer
    timer = threading.Timer(interval, onTick)
    timer.start()


if __name__ == "__main__":
    for k, v in processs.iteritems():
        recordLog("watching appliction: %s" % (v + k))
    
    timer = threading.Timer(interval, onTick)
    timer.start()
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值