cron作业是否杀死了最后一个cron执行

cron作业是否杀死了最后一个cron执行

本文介绍了cron作业是否杀死了最后一个cron执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cron作业执行一个PHP脚本。 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

您还可以使用一个简单的lockfile来确保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');

这篇关于cron作业是否杀死了最后一个cron执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 00:50