有没有创建pid并且进程无法启动的原因?

时间:2014-04-09 21:42:06

标签: 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()

我在shell(OSX)中做了:

python daemon.py start>>>>这运行但是没有创建pid文件(也尝试了sudo) python daemon.py stop>>>>返回错误:

  
    

Traceback(最近一次调用最后一次):文件“SST_daemon.py”,第68行,in         daemon_runner.do_action()文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/daemon/runner.py”,     第189行,在do_action中         func(self)File“/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'     没锁定

  

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

为什么这不起作用?

非常感谢任何帮助!!!!!

1 个答案:

答案 0 :(得分:2)

不确定为什么它不适合你,但也许下面的代码可能会清除一些东西。我确实需要即兴发挥,因为我没有你提到的myClass。我的垃圾邮件类中有一个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()中循环。这很有效,所以我在垃圾邮件中添加了.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