Run php script as daemon process

后端 未结 14 1710
-上瘾入骨i
-上瘾入骨i 2020-11-22 11:36

I need to run a php script as daemon process (wait for instructions and do stuff). cron job will not do it for me because actions need to be taken as soon as instruction arr

14条回答
  •  时光说笑
    2020-11-22 12:12

    Extending Emil Ivaov answer, You can do the following to close STDIN, STDOUT AND STDERROR in php

    if (!fclose(STDIN)) {
        exit("Could not close STDIN");
    }
    
    if (!fclose(STDOUT)) {
        exit("Could not close STDOUT");
    }
    
    if (!fclose(STDERR)) {
        exit("Could not close STDERR");
    }
    
    $STDIN = fopen('/dev/null', 'r');
    $STDOUT = fopen('/dev/null', 'w');
    $STDERR = fopen('/var/log/our_error.log', 'wb');
    

    Basically you close the standard streams so that PHP has no place to write. The following fopen calls will set the standard IO to /dev/null.

    I have read this from book of Rob Aley - PHP beyond the web

提交回复
热议问题