简体   繁体   中英

PHP Daemon Auto Restart on Fail

I have a PHP script running as a daemon. Every once in a while, I want the script to take a break and restart to clear up the memory usage when it receives the signal.

I've been looking at shell_exec(); to kill and restart the PHP script, however I was wondering if there was a cleaner method. I've also looked at wrapping the PHP script in a batch, and restarting it if needed, but I am only knowledgeable in PHP.

declare(ticks = 1);
    $processID = pcntl_fork();
    if ( $processID == -1 ) {
        echo "\n Error:  The process failed to fork. \n";
    } else if ( $processID ) {
        exit;
    } else {
    }
    if ( posix_setsid() == -1 ) {
        echo "\n Error: Unable to detach from the terminal window. \n";
    }
    $posixProcessID = posix_getpid();
    $filePointer = fopen( "/var/run/addy.pid" , "w" );
    fwrite( $filePointer , $posixProcessID );
    fclose( $filePointer );


gc_enable();
while (true) {

sleep(1);

print "debug: memory_get_peak_usage: ".memory_get_peak_usage()." debug: memory_get_usage: ".memory_get_usage()."\n";

   // STUFF GOES HERE

unset($array);
gc_collect_cycles();
    }

Thank you for your help!

One way would be to have a BASH script that would run your daemon.

#!/bin/bash

while [ 1 ]; do
  ./my_php_daemon
done

Then just exit your php daemon whenever you want it to restart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM