简体   繁体   English

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

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

How to automatic start and stop the Python APScheduler during Linux boot (Centos in my case), and stop it during shutdown? 如何在Linux启动期间自动启动和停止Python APScheduler(在我的情况下为Centos),并在关机期间停止它?

I can start a python script during boot in linux, but then how to stop it? 我可以在linux引导期间启动python脚本,但是如何停止它呢? (remember the PID?) (还记得PID吗?)

And I am wondering if that is the way to go, as I want to have an easy deployment, such that developers can easily update the files in test/production and restart the scheduler, without becoming a root such that they can start/stop the service. 我想知道这是否可行,因为我想轻松部署,以便开发人员可以轻松地更新测试/生产中的文件并重新启动调度程序,而不必成为root用户才能启动/停止服务。

Currently I have the scheduler started/stopped by using tmux, which works, but I can't seem to find a good way to improve that such that during a server start/stop it's automatically started/stopped and easy updated during a deployment :( 目前,我已经通过使用tmux启动/停止了调度程序,该方法可以正常工作,但是我似乎找不到一种改善这种情况的好方法,例如在服务器启动/停止过程中,它会自动启动/停止并在部署过程中易于更新:(

It's usual to create a file with extension .pid to hold the process PID. 通常创建一个扩展名为.pid的文件来保存进程PID。 Then you need to register a signal handler to cleanly exit, and make sure you delete the .pid file at exit. 然后,您需要注册一个信号处理程序以完全退出,并确保在退出时删除.pid文件。

For example: 例如:

#!/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