持久 PHP 套接字服务器
我正在计划开发一个用 PHP 编写的服务器,可以为套接字请求提供服务。我使用免费主机(Heliohost)进行测试,它有cPanel。到目前为止,我唯一能想到的让 PHP 脚本始终运行的方法是编写一个 cron 作业来运行 bash 脚本来检查 ps
以查看 PHP 是否已经在运行,如果没有,请启动它。
有更好的办法吗?也许有一种方法可以让 PHP 线程在 HTTP 请求上启动并在请求得到服务后继续在 Apache 中运行?
I'm planning the development of a server written in PHP that can service socket requests. I use a free host (Heliohost) for testing, and it has cPanel. So far the only thing I've been able to think of to have a PHP script always running is to write a cron job that runs a bash script to check ps
to see if the PHP is already running, and if it isn't, start it.
Is there a better way? Perhaps a way for a PHP thread to be started on an HTTP request and continue to run in Apache after the request has been serviced?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您几乎肯定不会成功地从 Apache 运行持久进程。它旨在防止这种情况(尽管如果您可以进行
fork(2)
系统调用,那么它可能是可行的)。但我不建议尝试。更有意义的是,如果您使用的托管提供商能够让您编写自己的 crontab(5) 规范并直接运行 PHP 解释器,那么更有意义。然后,您可以在
crontab(5)
中添加一行,例如:您的脚本应该执行通常的 守护进程 任务,以便
cron(8)
不会卡在等待进程退出。You will almost certainly not have success running persistent processes from Apache. It is designed to prevent that scenario (though if you can get to the
fork(2)
system call, it is probably do-able). I wouldn't recommend trying it though.What would make more sense is if you use a hosting provider that gives you the ability to write your own
crontab(5)
specifications and run the PHP interpreter directly. Then you could just add a line to yourcrontab(5)
like:Your script should probably perform the usual daemonization tasks so that
cron(8)
isn't stuck waiting for your process to exit.