连续运行Cron作业 [英] Running a Cron job continuously

查看:148
本文介绍了连续运行Cron作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我目前有一个PHP脚本,用于轮询数据库以进行下一步记录,它没有被更新。我的问题是这样:如果我然后更新数据库以显示记录正在处理,而cron作业正在运行(大约需要大约30秒),并且cron作业设置为每秒运行,在秒1 cron作业运行,但是这个作业会在第二次运行cron作业之前等待完成,否则它会在第二个时间运行?



感谢

解决方案

如果你使用cronjob,而不是在你的php脚本中使用任何类型的锁定cron将运行多个脚本副本,如果时间事件发生之前最后一个脚本完成。



为了避免这样的问题,你可以这样做:

 code> $ LOCK_FN =/tmp/my-script.lock
if(file_exists($ LOCK_FN)){
//上一个脚本没有完成退出。
die(上一个脚本还没有完成。
}
//创建锁
$ fp = fopen($ LOCK_FN,w);
fclose($ fp);

/ *

将您的脚本逻辑放在这里
* /

//打开锁定工作完成。
unlink($ LOCK_FN)


sorry if this question has been asked before, however I could not find anything relevant when searching.

I currently have a PHP script which polls a database for the next record which is has not been updated. My question is this: If I then update the database to show that the record is processing while the cron job is running (which takes approx 30 seconds), and the cron job is set to run every second, at second 1 the cron job runs, but will this job wait to complete before the cron job runs for the second time, or will it run at second 2?

Thanks

解决方案

If you are using cronjob and not using any kind of locking in your php script the cron will run more than one copy of the script if the time event occurs before the last script finished.

To avoid such issue you can do something like this:

$LOCK_FN = "/tmp/my-script.lock"
if(file_exists ($LOCK_FN)){
    //previous script is not finish exit.
    die("Previous script is not finished yet.");
}
//Create lock
$fp = fopen($LOCK_FN,"w");
fclose($fp);

/*

 Put your script logic here
 */

//Open the lock work is done.
unlink($LOCK_FN)

这篇关于连续运行Cron作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆