python - 没有创建 pid 并且进程没有启动的任何原因?

标签 python macos daemon pid python-daemon

我正在尝试将 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()

我做的外壳(OSX):

python daemon.py start >>>> 运行但未创建 pid 文件(也尝试过 sudo) python daemon.py stop >>>> 返回错误:

Traceback (most recent call last): File "SST_daemon.py", line 68, in daemon_runner.do_action() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/daemon/runner.py", line 189, in do_action func(self) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/daemon/runner.py", line 152, in _stop u"PID file %(pidfile_path)r not locked" % vars()) daemon.runner.DaemonRunnerStopFailureError: PID file '/Users/josefernandes/Documents/workspace/RandomThings/testdaemon.pid' not locked

我已经尝试解决这个问题几个小时,但到目前为止还没有结果。

这不起作用的任何原因?

非常感谢您的帮助!!!!

最佳答案

不知道为什么它对你不起作用,但也许下面的代码可能会澄清一些事情。我确实必须即兴创作,因为我没有你提到的我的类(class)。我的 Spam 类中有一个循环的 run() 方法:你的有吗?如果这样做,您可能会惊讶地发现 App.run() 中的循环不会循环,因为它保留在 myClass 中。但这是一个旁白。

由于我没有 OSX,因此我必须更改一些路径。但我尝试不改变任何其他内容,下面的代码对我有用:

  1 #standard python libs
  2 import logging
  3 import time
  4 
  5 #third party libs
  6 from daemon import runner
  7 import spam.eggs
  8 
  9 class App():
 10 
 11     def __init__(self):
 12         self.stdin_path = '/dev/null'
 13         self.stdout_path = '/dev/tty'
 14         self.stderr_path = '/dev/tty'
 15         self.pidfile_path = '/home/fbicknel/tmp/testdaemon.pid'
 16         self.pidfile_timeout = 5
 17 
 18     def run(self):
 19         while True:
 20             #Note that logger level needs to be set to logging.DEBUG before this shows up in the logs
 21             logger.debug("Debug message")
 22             logger.info("Info message")
 23             logger.warn("Warning message")
 24             logger.error("Error message")
 25             #Main code goes here ...
 26             # sst=myClass()
 27             # sst.run()
 28             sst=spam.eggs.Eggs()
 29             sst.run()
 30             time.sleep(10)
 31 
 32 app = App()
 33 logger = logging.getLogger("DaemonLog")
 34 logger.setLevel(logging.INFO)
 35 formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
 36 handler = logging.FileHandler("/home/fbicknel/tmp/testdaemon.log")
 37 handler.setFormatter(formatter)
 38 logger.addHandler(handler)
 39 
 40 daemon_runner = runner.DaemonRunner(app)
 41 #This ensures that the logger file handle does not get closed during daemonization
 42 daemon_runner.daemon_context.files_preserve=[handler.stream]
 43 daemon_runner.do_action()

我尝试开始的一件事是完全消除 myClass 并让 run() 中的循环执行循环。这很有效,所以我添加了 spam.Eggs。

希望这能让你再次前进。

下面是我在 spam/eggs.py 中的模块:

  1 import time
  2 import logging
  3 
  4 class Eggs(object):
  5     def __init__(self, startvalue='green'):
  6         " init a spam object "
  7         self.logger = logging.getLogger("DaemonLog")
  8         self.color  = startvalue
  9 
 10     @property
 11     def color(self):
 12         return self._color
 13         
 14     @color.setter
 15     def color(self, value): 
 16         self._color = value
 17     
 18     def run(self):
 19         ' Just loop sounding happy. '
 20         while True:
 21             self.logger.info("yippie kai ai o")
 22             time.sleep(20)
 23             
 24 if __name__ == "__main__":
 25     spamalot = Eggs()
 26     print spamalot.color
 27     spamalot.color = 42
 28     print spamalot.color

关于python - 没有创建 pid 并且进程没有启动的任何原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22974503/

相关文章:

node.js - 如何在没有管理员的情况下在 Mac 上下载 Node.js

macos - 如何在磁盘镜像上挂载特定的文件系统?

c - 调用daemon()和调用fork()、setsid()、fork()等有什么区别?

perl - 制作一个运行24/7并从命名管道读取的Perl守护程序

python - 用于规范层事件/权重的 add_loss() 在tensorflow.keras 2.0更新中不再起作用

python - 带有 HiddenInput 的 Django 的 ModelForm 返回无效

macos - 安装 tensorflow 时出现错误 setuptools

node.js - 如何将 node.js 应用程序作为后台服务运行?

python - 使用 BeautifulSoup 抓取具有多个页面的表

python根据日期创建目录结构