문제

I am building a simple pyhon daemon based on Sander Marechal's code. Daemon's whole purpose is to run a php file every second (php file loops through database checking values and updating database). Problem arises on the section

subprocess.call(['php','test.php'])

I can run "php test.php" on shell and it does what it is suppose to do but when it is called periodically from the daemon it doesn't seem to be executed. I also know daemon works on the background via checking running process ps aux | grep "daemon-example" also i included a do_something function which records every time function executed and appends time to a text file.

#!/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)
도움이 되었습니까?

해결책

The script you are trying to run is not executed because the working directory is the root directory ('/') and that's because of this piece of code:

# decouple from parent environment
os.chdir("/")

So actually your code tries to execute: python /test.py(which does not exist) and not 'your_current_directory/test.py'.
To fix it either remove os.chdir("/"), or provide the full path to the file like so:

subprocess.call(['python','my_full_path_to_working_directory/test.py'])
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top