首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >linux启动时APScheduler的自动启动和停止?

linux启动时APScheduler的自动启动和停止?
EN

Stack Overflow用户
提问于 2018-01-30 01:29:26
回答 1查看 395关注 0票数 0

如何在Linux启动时自动启动和停止Python APScheduler (我的例子是Centos),并在关机时停止它?

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

我想知道这是不是应该这样做,因为我想要有一个简单的部署,这样开发人员就可以在测试/生产中轻松地更新文件并重新启动调度程序,而不会成为根目录,这样他们就可以启动/停止服务。

目前我已经使用tmux启动/停止了调度程序,这是有效的,但我似乎找不到一个好的方法来改进它,以便在服务器启动/停止期间自动启动/停止,并在部署期间轻松更新:(

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-30 02:20:44

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

例如:

代码语言:javascript
复制
#!/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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48506824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档