【发布时间】:2014-04-09 21:42:06
【问题描述】:
我正在尝试将此脚本与 python-daemon 一起使用,以在后台启动和停止某些代码的运行。 (最终目标是在 AWS 实例中使用它)。
由于某些让我无法理解的原因,没有生成 pid 文件,我认为该进程没有运行。
Python 脚本:
#standard python libs
import logging
import time
#third party libs
from daemon import runner
class App():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/Users/NAME/Documents/workspace/RandomThings/testdaemon.pid'
self.pidfile_timeout = 5
def run(self):
while True:
#Note that logger level needs to be set to logging.DEBUG before this shows up in the logs
logger.debug("Debug message")
logger.info("Info message")
logger.warn("Warning message")
logger.error("Error message")
#Main code goes here ...
sst=myClass()
sst.run()
time.sleep(10)
app = App()
logger = logging.getLogger("DaemonLog")
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler = logging.FileHandler("/Users/NAME/Documents/workspace/RandomThings/testdaemon.log")
handler.setFormatter(formatter)
logger.addHandler(handler)
daemon_runner = runner.DaemonRunner(app)
#This ensures that the logger file handle does not get closed during daemonization
daemon_runner.daemon_context.files_preserve=[handler.stream]
daemon_runner.do_action()
我在 shell (OSX) 中做的:
python daemon.py start >>>> 这会运行,但没有创建 pid 文件(也尝试过 sudo) python daemon.py stop >>>> 返回错误:
Traceback(最近一次调用最后一次):文件“SST_daemon.py”,第 68 行,在 daemon_runner.do_action() 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/daemon/runner.py”, 第 189 行,在 do_action func(自我)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/daemon/runner.py”, 第 152 行,在 _stop u"PID 文件 %(pidfile_path)r 未锁定"% vars()) daemon.runner.DaemonRunnerStopFailureError: PID 文件 '/Users/josefernandes/Documents/workspace/RandomThings/testdaemon.pid' 未锁定
我已经尝试修复这个问题几个小时,但到目前为止没有结果。
这不起作用的任何原因?
非常感谢任何帮助!!!!!!
【问题讨论】:
标签: python macos daemon pid python-daemon