PHP 守护进程失败时自动重新启动

发布于 2024-10-14 00:45:07 字数 911 浏览 4 评论 0原文

我有一个 PHP 脚本作为守护进程运行。每隔一段时间,我希望脚本在收到信号时休息一下并重新启动以清除内存使用情况。

我一直在研究 shell_exec();杀死并重新启动 PHP 脚本,但是我想知道是否有更干净的方法。我还考虑过批量包装 PHP 脚本,并在需要时重新启动它,但我只了解 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();
    }

感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2024-10-21 00:45:07

一种方法是使用 BASH 脚本来运行您的守护程序。

#!/bin/bash

while [ 1 ]; do
  ./my_php_daemon
done

然后只要你想重新启动你的 php 守护进程就退出它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文