【问题标题】:How to run python daemon process on Linux boot如何在 Linux 启动时运行 python 守护进程
【发布时间】:2018-02-21 19:19:51
【问题描述】:

我有以下简单的python脚本test.py(玩具示例)

#!/usr/bin/python
import time
from time import gmtime, strftime
from daemon import runner

import config
import subprocess


class App():
def __init__(self):
    self.stdin_path = '/dev/null'
    self.stdout_path = '/dev/tty'
    self.stderr_path = '/dev/tty'
    self.pidfile_path =  '/tmp/foo.pid'
    self.pidfile_timeout = 5
def run(self):
    while True:
        file = open('/var/www/myfile.dat'+strftime("%Y-%m-%d %H:%M:%S", gmtime()), 'w+')
        file.write("Hello World")
        file.close()            

        time.sleep(20)

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

我想在 Linux Centos 7 启动时运行该脚本。 出于这个原因,我在rc.local 文件中添加了这一行

  touch python /root/scripts/test.py start

但不幸的是脚本没有运行。

附:当我从命令行python test.py start 运行该脚本时,它可以完美运行。

【问题讨论】:

标签: python centos7 daemon boot


【解决方案1】:

创建一个 cron 作业。查看link,了解如何在 CentOS 上设置 cron 作业。然后你可以调用python脚本 @reboot sleep 60 && /usr/bin/python /path/to/script.py >> /path/to/log/file.log

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 2020-12-28
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多