emden-norfolk / php-daemon
此包已被放弃,不再维护。作者建议使用 https://openswoole.com/docs/modules/swoole-process-daemon 包代替。
使用优雅的关闭和多个工作进程使PHP脚本成为守护进程。
v1.0.4
2018-03-25 05:48 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2023-02-18 04:35:04 UTC
README
使用优雅的关闭和多个工作进程使PHP脚本成为守护进程。
使用方法
如何编写脚本。
编写将被phpdaemon调用的PHP脚本时必须遵守以下规则
- 您的脚本必须实现一个函数
main()
。 - 您的脚本必须检查
running()
函数对于每个工作项的真假条件。如果为假,则脚本应该退出。如果不检查此条件,则脚本将无法干净地退出,并且信号中断将被忽略。 - 标准输出已关闭。任何使用
print
、echo
、var_dump
等的操作都将导致脚本突然终止。 - 错误将被记录到日志文件中。使用
trigger_error()
手动写入日志。
请参阅 examples/basic.php
以获取实现参考。
独立
./phpdaemon <php file> <process id file> <log file> <worker number>
示例
./phpdaemon examples/basic.php example.0.pid example.log 0
Monit
配置Monit以使用两个工作进程的示例。假设用户是 apache
并且目录是 /opt
。
check process example0 with pidfile /opt/daemon/example.0.pid
start program = "/opt/daemon/phpdaemon /opt/daemon/examples/basic.php /opt/daemon/example.0.pid /opt/daemon/example.log 0"
as uid apache and gid apache
stop program = "/opt/daemon/phpdaemonstop /opt/daemon/example.0.pid"
check process example1 with pidfile /opt/daemon/example.1.pid
start program = "/opt/daemon/phpdaemon /opt/daemon/examples/basic.php /opt/daemon/example.1.pid /opt/daemon/example.log 1"
as uid apache and gid apache
stop program = "/opt/daemon/phpdaemonstop /opt/daemon/example.1.pid"
Monit 和 Laravel
请参阅 examples/laravel/
资源
- UNIX环境高级编程(第三版),第13章:守护进程,W. R. Stevens等,2013。
- 在POSIX系统上使PHP CLI脚本成为守护进程,Andy Thompson,2010。