cron 作业是否会终止最后的 cron 执行?
我有一个执行 PHP 脚本的 cron 作业.cron 设置为每分钟运行一次,这仅用于测试目的.它正在执行的 PHP 脚本旨在将用户上传到服务器的视频转换为 Flash 格式(例如....flv).通过命令行手动执行脚本时,脚本执行良好,但是通过 cron 执行时,它开始正常,但一分钟后它就停止了.
I have a cron job the executes a PHP script. The cron is setup to run every minute, this is done only for testing purposes. The PHP script it is executing is designed to convert videos uploaded to the server by users to a flash format (eg... .flv). The script executes fine when manually doing it via command line, however when executing via cron it starts fine but after one minute it just stops.
似乎在执行下一个 cron 时它会杀死"最后一个 cron 执行.我添加了以下 PHP 函数:
It seems that when the next cron is executed it "kills" the last cron execution. I added the following PHP function:
ignore_user_abort(true);
希望它不会中止最后一次执行,我测试将 cron 设置为每 5 分钟运行一次,效果很好,但是视频的转换可能需要 5 分钟以上,所以我需要弄清楚为什么它在什么时候停止另一个 cron 被执行.
In hopes that it would not abort the last execution, I tested setting the cron to run every 5 minutes, which works fine, however a conversion of a video may take over 5 minutes so I need to figure out why its stoping when another cron is executed.
任何帮助将不胜感激.
谢谢!
我的 cron 看起来像:
My cron looks like:
*/1 * * * * php /path_to_file/convert.php
推荐答案
我认为 cron 不会杀死任何进程.但是,cron 并不真正适合长时间运行的进程.此处可能发生的情况是,您的脚本在多次执行时会自行踩踏.例如,两个 PHP 进程可能同时尝试写入同一个文件.
I don't think cron kills any processes. However, cron isn't really suitable for long running processes. What may be happening here is that your script tramples all over itself when it is executed multiple times. For example, both PHP processes may be trying to write to the same file at the same time.
首先,确保您不仅查看 php 错误日志,还要尝试从 PHP 文件本身捕获输出.例如:
First, make sure you not only look in the php error log but also try to capture output from the PHP file itself. E.g:
*/1 * * * * * php /path/to/convert.php & >> /var/log/convert.log
您还可以使用简单的锁定文件来确保 convert.php 不会被多次执行.类似的东西:
You could also use a simplistic lockfile to ensure that convert.php isn't executed multiple times. Something like:
if (file_exists('/tmp/convert.lock')) {
exit();
}
touch('/tmp/convert.lock');
// convert here
unlink('/tmp/convert.lock');
相关文章