持续运行 PHP 脚本等待视频转码
我正在制作一个转码服务器,它使用 FFMPEG 将视频转换为 flv。用户上传视频后,视频将在亚马逊简单队列服务中排队等待处理。系统是linux ubuntu。
我想知道是否可以连续运行多个 PHP 脚本(下载排队文件、处理下载的文件等),而不是每 1 分钟运行一次 CRON。他们每个人都有自己的队列,每 10 秒左右就会读取一次以寻找新任务。
我的问题是:
如何检测脚本是否已经在运行?我每 1 分钟运行一次 CRON,如果其中一个程序无法运行,我会再次加载它。类似的事情在linux上是如何完成的? PID 文件?
感谢您的帮助, 伊安
I'm making a transcoding server which uses FFMPEG to convert videos to flv. After user uploads a video it's queued for processing in amazon Simple Queue Service. System is linux ubuntu.
Instead of running CRON each 1min I wonder if it would be possible to continously run several PHP scripts (dowload queued files, process downloaded etc). Each of them would have its own queue which would be read every 10s or so looking for new tasks.
My question is:
How to detect if the script is already running? I'd run CRON each 1min and if one of the programs would not be running I'd load it again. How stuff like that is done on linux? PID files?
thanks for help,
ian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您可以使用 PID 文件
或者临时表,或者内存缓存等,
但我确实喜欢这样:
通过 cron 运行执行视频转换的脚本,并检查进程是否终止
这个 cron 脚本获取需要从数据库或文件转换的电影
Yes, you can use PID files
Or temporary table, or memcache, e.t.c.
But I do like this:
By cron runs script that execute convert video, and it check if process is terminated or not
This cron script get movie what needs to convert from database or file
PHP 的 PEAR 存储库有一个
System_Daemon
类,用于从 PHP 创建守护程序。我已经将它用于几个系统并取得了良好的效果。PHP's PEAR repository has a
System_Daemon
class for creating daemons out of PHP. I've used it for a couple systems with good results.我专门针对此问题创建了一个类似的脚本
检查:https://github.com/SirNarsh/EasyCron
想法是将脚本的 PID 保存到文件中,然后通过检查 /proc/PID 是否存在来检查进程是否正在运行
I've created a similar script specifically for this issue
Check: https://github.com/SirNarsh/EasyCron
The idea is to save PID of the script to a file, then check if the process is running by checking /proc/PID existence
我可能会选择基于 gearman< 的解决方案,而不是仅使用纯 PHP 来完成此操作/a> (引用wikipedia):
由于 gearman 扩展,它与 PHP 配合得很好,并且会为您处理大部分排队的事情。
请注意,当您有更多视频需要转码时,它也会让事情变得更容易,从而更容易扩展到多个服务器。
Instead of doing this with only pure-PHP, I would probably go with a solution based on gearman (quoting wikipedia) :
It works well with PHP, thanks to the gearman extension, and will deal with most of the queuing stuff for you.
Note that it'll also facilitate things when you have more videos to transcode, making scaling to several servers easier.