我正在构建一个基于Sander Marechal's代码的简单pyhon守护进程。守护进程的全部目的是每秒运行一个php文件(php文件循环通过数据库检查值和更新数据库)。问题出现在
部分subprocess.call([ 'PHP的', 'test.php的'])
我可以在shell上运行“php test.php”并且它会执行它想要做的事情,但是当从守护程序定期调用它时它似乎没有被执行。我也知道守护进程通过检查运行进程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)
答案 0 :(得分:0)
您尝试运行的脚本未执行,因为工作目录是根目录('/')
,这是因为这段代码:
# decouple from parent environment
os.chdir("/")
所以实际上你的代码试图执行:python /test.py
(不存在)而不是'your_current_directory/test.py'
。
要解决此问题,请删除os.chdir("/")
,或提供文件的完整路径,如下所示:
subprocess.call(['python','my_full_path_to_working_directory/test.py'])