繁体   English   中英

在Linux启动期间自动启动和停止APScheduler?

[英]Automatic start and stop of APScheduler during boot of linux?

如何在Linux启动期间自动启动和停止Python APScheduler(在我的情况下为Centos),并在关机期间停止它?

我可以在linux引导期间启动python脚本,但是如何停止它呢? (还记得PID吗?)

我想知道这是否可行,因为我想轻松部署,以便开发人员可以轻松地更新测试/生产中的文件并重新启动调度程序,而不必成为root用户才能启动/停止服务。

目前,我已经通过使用tmux启动/停止了调度程序,该方法可以正常工作,但是我似乎找不到一种改善这种情况的好方法,例如在服务器启动/停止过程中,它会自动启动/停止并在部署过程中易于更新:(

通常创建一个扩展名为.pid的文件来保存进程PID。 然后,您需要注册一个信号处理程序以完全退出,并确保在退出时删除.pid文件。

例如:

#!/usr/bin/env python

import signal
import atexit
import os

PID_FILE_PATH = "program.pid"
stop = False

def create_pid_file():
    # this creates a file called program.pid
    with open(PID_FILE_PATH, "w") as fhandler:
        # and stores the PID in it
        fhandler.write(str(os.getpid()))

def sigint_handler(signum, frame):
    print("Cleanly exiting")
    global stop

    # this will break the main loop
    stop = True

def exit_handler():
    # delete PID file
    os.unlink(PID_FILE_PATH)

def main():
    create_pid_file()

    # this makes exit_handler to be called automatically when the program exists
    atexit.register(exit_handler)

    # this makes sigint_handler to be called when a signal SIGTERM 
    # is sent to the process, e.g with command: kill -SIGTERM $PID
    signal.signal(signal.SIGTERM, sigint_handler)

    while not stop:
        # this represents your main loop
        pass

if __name__ == "__main__":
    main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM