PHP 守护进程失败时自动重新启动
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是使用 BASH 脚本来运行您的守护程序。
然后只要你想重新启动你的 php 守护进程就退出它。
One way would be to have a BASH script that would run your daemon.
Then just exit your php daemon whenever you want it to restart.