我正在基于Sander Marechal's代码构建一个简单的pyhon守护程序。守护进程的全部目的是每秒运行一个php文件(php文件通过数据库循环检查值和更新数据库)。该部分出现问题subprocess.call(['php','test.php'])
我可以运行“php测试.php“在shell上,它执行它应该做的事情,但是当从守护进程周期性地调用它时,它似乎没有被执行。我还知道后台程序通过检查运行进程ps aux | grep“daemon example”在后台工作,我还包括了一个do_something函数,它记录每个函数执行的时间并将时间附加到文本文件中。在#!/usr/bin/env python
import sys, time,subprocess
from daemon import Daemon
def runphp():
#subprocess.call(['php ~/pydaemon/test.php'], shell=True)
subprocess.call(['python', 'test.py'])
def do_something():
with open("/tmp/current_time.txt",'a') as f:
f.write("The time is now\n" + time.ctime())
class MyDaemon(Daemon):
def run(self):
while True:
time.sleep(1)
do_something()
subprocess.call(['php','test.php'])
#runphp()
if __name__ == "__main__":
daemon = MyDaemon('/tmp/daemon-example.pid')
if len(sys.argv) == 2:
if 'start' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()
else:
print "Unknown command"
sys.exit(2)
sys.exit(0)
else:
print "usage: %s start|stop|restart" % sys.argv[0]
sys.exit(2)