长时间运行的 php 脚本作为 cron 失败
我编写了一个 PHP 脚本,使用 ftp_put 将图像从目录上传到远程服务器。我已使用任务调度程序和 wget 将其设置为 cron。最初它工作得很好,但过了一段时间,不知道具体什么时候,进程冻结了,“Windows 任务计划程序”说它正在运行该作业,但不再上传照片。
最初我认为问题是由于 max_execution_time 造成的,但我通过使用 set_time_limit(3600*24); 将其设置为 24 小时;我已将 max_input_time 设置为 600 秒(10 分钟)。
为什么不完成任务呢?
这是代码:
if($conn){
if (is_dir($imagesPath)){
if($files = opendir($imagesPath)){
while(($file = readdir($files)) !== false){
if($file != "." && $file != ".." && preg_match("/\.jpg|\.JPG|\.gif|\.GIF|\.png|\.PNG|\.bmp|\.BMP/",$file) && date("Ymd",filemtime($imagesPath.'/'.$file)) >= date("Ymd",strtotime(date("Y-m-d")." -".$days." day"))){
if(ftp_put($conn, $remotePath.$file, $imagesPath.'/'.$file, FTP_BINARY)){
//echo $file;
$counter++;
}
else{
echo '<br>'.$imagesPath.'/'.$file;
}
}
}
closedir($files);
echo $counter.' Files Uploaded on '.date("Y-m-d");
}
else{
echo 'Unable to read '.$imagesPath;
}
}
else{
echo $imagesPath.' Does not exist';
}
ftp_close($conn);
}else{
echo "Failed to connect";
}
/* End */
exit;
已添加:
/* Settings */
// Set Max Execution time
set_time_limit(3600*24);
在脚本顶部。
谢谢。
I have written a PHP script that uploads images from a directory to a remote server using ftp_put. I have set this as a cron using task scheduler and wget. Initially it works great, but then after a while, do not know exactly when, the process freezes, by that "windows task scheduler" saying it is running the job, but no photos are no longer being uploaded.
Initially I thought the problem was due the max_execution_time , but I have set that to 24 hours by using set_time_limit(3600*24); and I have set max_input_time to 600 seconds (10 mins).
Why doesn't it complete the task?
Here is the code:
if($conn){
if (is_dir($imagesPath)){
if($files = opendir($imagesPath)){
while(($file = readdir($files)) !== false){
if($file != "." && $file != ".." && preg_match("/\.jpg|\.JPG|\.gif|\.GIF|\.png|\.PNG|\.bmp|\.BMP/",$file) && date("Ymd",filemtime($imagesPath.'/'.$file)) >= date("Ymd",strtotime(date("Y-m-d")." -".$days." day"))){
if(ftp_put($conn, $remotePath.$file, $imagesPath.'/'.$file, FTP_BINARY)){
//echo $file;
$counter++;
}
else{
echo '<br>'.$imagesPath.'/'.$file;
}
}
}
closedir($files);
echo $counter.' Files Uploaded on '.date("Y-m-d");
}
else{
echo 'Unable to read '.$imagesPath;
}
}
else{
echo $imagesPath.' Does not exist';
}
ftp_close($conn);
}else{
echo "Failed to connect";
}
/* End */
exit;
Added:
/* Settings */
// Set Max Execution time
set_time_limit(3600*24);
at the top of the script.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在做类似的事情,我发现以下内容可以提供帮助:
1)确保每个步骤都是有条件的,因此如果某些内容无法加载(如图像、ftp 连接等),程序将继续运行(迭代)。
2)在文件末尾(或困难步骤之间)设置一个小的
sleep()
。我也将它用于图像,当与图像的连接滞后或“图像写入”时间滞后时,它会有所帮助。3) 将调度程序设置为经常执行脚本(我的是每天 2 次),但只要选中此框,就可以将其设置为每小时执行一次:如果脚本已在运行,则不要启动新实例
4) 根据服务器的设置,检查可能中断脚本运行时的其他任务。这并不总是 PHP 问题。只要您正确安排了重新执行脚本的任务,就应该没问题。
5)为了方便调试,使用简单的文件日志记录(
$message = fopen($myLogFile, 'w');
)代替 echo 语句(最有可能显示在 cmd 窗口中)除了“不存在”或“连接失败”语句之外,还应包含更多详细信息,以便在失败时,您可以转到日志文件并查看失败的时间和原因。6) 您可以尝试使用无限循环,例如 (
while (true) { ... your code... }
) 而不是set_time_limit()
。我希望这有帮助:)
I am working on something similar and I found the following to help:
1) make sure every step is conditional so if something fails to load (like an image, ftp connection etc.) the procedure keeps running (iterating).
2) set a small
sleep()
at the end of the file (or between difficult steps). I am using it for images as well and when the connection to the image lags or the 'image write' time lags it helps.3) set the scheduler to execute the script often (my is 2 times a day) but it could be set to hourly as long as you check the box: do not start new instance if the script is already running
4) depending on the setup of your server, check for other tasks which may interrupt the script run-time. It is not always PHP issue. As long as you have properly scheduled task to re-execute the script you should be fine.
5) for easy debugging, instead of the echo statement (which is most likely showing in your cmd window) use a simple file logging like (
$message = fopen($myLogFile, 'w');
) additionally to your "Does not exist" or "Failed to connect" statements and include more details so when it fails, you can go to your log file and see when and why it failed.6) you may try to use an infinite loop like (
while (true) { ... your code... }
) instead ofthe set_time_limit()
.I hope this helps :)