1. 程式人生 > 實用技巧 >Python 利用fork建立守護程序

Python 利用fork建立守護程序

寫一個指令碼模擬監控推送元件,假設每分鐘將ok的狀態寫入檔案一次,並且以守護程序方式執行; apscheduler是一個定時任務模組

#!/bin/env python3
#-*- coding: utf-8 -*-
import os
import sys
import atexit
import time
from apscheduler.schedulers.blocking import BlockingScheduler


def daemonize(pid_file=None):
    # 判斷程式是否已經啟動(不是必要的)
    if pid_file is None:
        pid_file = os.path.join("/tmp", ".".join((os.path.basename(__file__).split(".")[0], "pid")))
    if os.path.isfile(pid_file):
        print("%s 檔案已存在" % pid_file)
        sys.exit(100)

    # 第一次fork
    pid = os.fork()
    if pid:
        sys.exit(0)
    # 子程序預設繼承父程序的工作目錄,最好是變更到根目錄,否則回影響檔案系統的解除安裝
    os.chdir('/')
    # 子程序預設繼承父程序的umask(檔案許可權掩碼),重設為0(完全控制),以免影響程式讀寫檔案
    os.umask(0)
    # 讓子程序成為新的會話組長和程序組長
    os.setsid()

    _pid = os.fork()
    if _pid:
        sys.exit(0)

    # 重新整理緩衝區 
    sys.stdout.flush()
    sys.stderr.flush()

    # dup2函式原子化地關閉和複製檔案描述符,重定向到/dev/nul,即丟棄所有輸入輸出
    with open('/dev/null') as read_null, open('/dev/null', 'w') as write_null:
        os.dup2(read_null.fileno(), sys.stdin.fileno())
        os.dup2(write_null.fileno(), sys.stdout.fileno())
        os.dup2(write_null.fileno(), sys.stderr.fileno())

    # 寫入pid檔案
    with open(pid_file, 'w') as f:
        f.write(str(os.getpid()))

    # 註冊退出函式,程序退出時移除pid檔案
    atexit.register(os.remove, pid_file)


def proc():
    os.popen("echo 'ok' >> /tmp/file")


if __name__ == "__main__":
    daemonize()
    sched = BlockingScheduler()
    sched.add_job(proc, "interval", seconds=60)
    sched.start()