문제

프로그램이 중단되거나 Python을 사용하여 실행되지 않을 때를 감지하고 다시 시작해야합니다. Python 모듈에 반드시 의존하지 않는 메소드가 부모 프로세스 인 방법이 필요합니다.

본인이 본질적으로하는 while 루프 구현을 고려하고 있습니다

ps -ef | grep process name

프로세스를 찾지 못하면 다른 프로세스가 시작됩니다. 아마도 이것은 가장 효율적인 방법이 아닙니다. 나는 Python을 처음 접 했으므로 이미이 작업을 수행하는 Python 모듈이있을 수 있습니다.

도움이 되었습니까?

해결책

왜 직접 구현합니까? 기존 유틸리티와 같은 유틸리티 악마 또는 데비안의 start-stop-daemon 장기 생활 서버 프로세스를 실행하는 것에 대해 다른 어려운 것들을 올바르게 얻을 가능성이 높습니다.

어쨌든 서비스를 시작할 때 PID를 넣으십시오. /var/run/<name>.pid 그리고 당신을 만드십시오 ps 명령은 해당 프로세스 ID를 찾아 올바른 프로세스인지 확인하십시오. Linux에서는 단순히 볼 수 있습니다 /proc/<pid>/exe 확인하려면 오른쪽 실행 파일을 가리 킵니다.

다른 팁

초기를 재발 명하지 마십시오. 귀하의 OS에는 시스템 리소스가 거의 필요하지 않으며 재현 할 수있는 것보다 확실히 더 잘 수행 할 수있는 기능이 있습니다.

클래식 리눅스에는 /etc /inittab이 있습니다

ubuntu는 /etc/event.d (신생)가 있습니다.

OS X가 시작되었습니다

Solaris에는 SMF가 있습니다

다음 코드는 주어진 간격으로 주어진 프로세스를 확인하고 다시 시작합니다.

#Restarts a given process if it is finished.
#Compatible with Python 2.5, tested on Windows XP.
import threading
import time
import subprocess

class ProcessChecker(threading.Thread):
    def __init__(self, process_path, check_interval):
        threading.Thread.__init__(self)
        self.process_path = process_path
        self.check_interval = check_interval

    def run (self):
        while(1):
            time.sleep(self.check_interval)
            if self.is_ok():
                self.make_sure_process_is_running()

    def is_ok(self):
        ok = True
        #do the database locks, client data corruption check here,
        #and return true/false
        return ok

    def make_sure_process_is_running(self):
        #This call is blocking, it will wait for the
        #other sub process to be finished.
        retval = subprocess.call(self.process_path)

def main():
    process_path = "notepad.exe"
    check_interval = 1 #In seconds
    pm = ProcessChecker(process_path, check_interval)
    pm.start()
    print "Checker started..."

if __name__ == "__main__":
    main()

아마 당신은 필요합니다 http://supervisord.org

나는 그것을 직접 시도하지 않았지만 파이썬 시스템 정보 프로세스를 찾고 정보를 얻는 데 사용할 수있는 모듈. afair a ProcessTable 실행중인 프로세스를 검사하는 데 사용할 수있는 클래스이지만 잘 문서화되지 않은 것 같습니다 ...

나는 당신이 초 또는 2 초마다 만 확인하는 한 명령 줄 경로 (IMHO가 더 쉬운 IMHO)를 갈 것입니다. 리소스 사용량은 10 세 미만의 시스템에서 사용 가능한 처리와 비교하여 무한이어야합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top