繁体   English   中英

如何从应用程序内部重新启动python应用程序

[英]How do I restart a python app from within the app itself

获得了一个Django应用程序,该应用程序的用户正在通过设置向导。 向导完成后,该应用需要重新启动才能选择必要的设置。

该应用程序在Ubuntu 18.04上运行,并使用supervisor进行监控

理想情况下,我希望从Django应用本身调用systemctl restart administratord.service

所以我尝试了

import subprocess
subprocess.run("systemctl restart supervisord.service")

但是,此操作失败并显示以下错误:

FileNotFoundError [Errno 2]没有这样的文件或目录:'systemctl restartsupervised.service':'systemctl重启supervised.service'

这个问题在这里SO但是这是一个老问题,答案有依靠操作系统。*而作为此张贴似乎是首选的方法或访问OS功能

看到我的错误。 该命令应以以下方式运行:

subprocess.run(["systemctl", "restart", "supervisor.service"])

来源: http//queirozf.com/entries/python-3-subprocess-examples

对于更干净的方式来启动自己的python程序,将是:-

import os
import sys
import psutil
import logging

def restart_program():
    """Restarts the current program, with file objects and descriptors
       cleanup
    """

    try:
        p = psutil.Process(os.getpid())
        for handler in p.get_open_files() + p.connections():
            os.close(handler.fd)
    except Exception, e:
        logging.error(e)

    python = sys.executable
    os.execl(python, python, *sys.argv)

暂无
暂无

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

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