按计划任务运行的PHP脚本挂起 - 帮助!

时间:2021-04-02 02:21:23

I've built a php script that runs from the command line. It opens a connection into a pop3 email account and downloads all the emails and writes them to a database, and deletes them once downloaded. I have this script being called from the commandline by a bat file. in turn I have created a scheduled task which invokes the bat file every 5 minutes. The thing is that I have set the time out to zero for the fact that at times there could be emails with large attachments and the script actually downloads the attachments and stores them as raw files offline and the no timeout is so that the script doesnt die out during downloading.

我已经构建了一个从命令行运行的php脚本。它打开一个连接到pop3电子邮件帐户并下载所有电子邮件并将其写入数据库,并在下载后删除它们。我有一个bat文件从命令行调用此脚本。反过来,我创建了一个计划任务,每5分钟调用一次bat文件。事实上,我已经把时间设置为零,因为有时可能会有大附件的电子邮件,并且脚本实际上下载了附件并将它们作为原始文件存储离线,并且没有超时是因为脚本不会死在下载过程中。

I've found that the program hangs sometimes and its a bit annoying at that - it always hangs are one point i.e. when negotiating the connection and getting connected to the mail server. And because the timeout is set to zero it seems to stay stuck up in taht position. And because of that the task is not run as its technically hung up.

我发现该程序有时会挂起并且有点烦人 - 它总是挂起是一点,即在协商连接并连接到邮件服务器时。并且因为超时设置为零,所以它似乎一直处于停滞状态。因此,任务不会因技术上的问题而停止运行。

I want that the program should not timeout when downloading emails - however at the points where it is negotiating a connection or trying to connect to the mailserver there should be a timeout only at that point itself and not the rest of the program execution.

我希望程序在下载电子邮件时不应该超时 - 但是在它正在协商连接或尝试连接到邮件服务器时,应该只在该点本身超时,而不是程序执行的其余部分。

How do I do this :(

我该怎么做呢 :(

2 个解决方案

#1


The timeout value can be set using socket_set_timeout() if you're using sockets directly. You can have a look at this other question for details. If you're using a library, you should check its docs. But I would rather check if it really is the timeout issue. Log php errors into a file and you can browse them all. Set the max_execution_time to some acceptable value (like 10 minutes) and check the logs when it hangs again, if it's always the same line where it's failing.

如果您直接使用套接字,则可以使用socket_set_timeout()设置超时值。您可以查看其他问题以获取详细信息。如果您使用的是库,则应检查其文档。但我宁愿检查它是否真的是超时问题。将php错误记录到文件中,您可以浏览它们。将max_execution_time设置为某个可接受的值(例如10分钟),并在再次挂起时检查日志,如果它始终与它失败的行相同。

#2


I ran into this problem recently for those who are looking for an answer. I had task scheduler calling the .php file directly. It would hang and never return anything.

我最近遇到了那些正在寻找答案的人遇到这个问题。我让任务调度程序直接调用.php文件。它会挂起,永远不会返回任何东西。

I found that running firefox.exe and sending the url as a parameter was the proper way to run a .php file through task manager.

我发现运行firefox.exe并将url作为参数发送是通过任务管理器运行.php文件的正确方法。

firefox.exe "http://host/dir/file.php"

#1


The timeout value can be set using socket_set_timeout() if you're using sockets directly. You can have a look at this other question for details. If you're using a library, you should check its docs. But I would rather check if it really is the timeout issue. Log php errors into a file and you can browse them all. Set the max_execution_time to some acceptable value (like 10 minutes) and check the logs when it hangs again, if it's always the same line where it's failing.

如果您直接使用套接字,则可以使用socket_set_timeout()设置超时值。您可以查看其他问题以获取详细信息。如果您使用的是库,则应检查其文档。但我宁愿检查它是否真的是超时问题。将php错误记录到文件中,您可以浏览它们。将max_execution_time设置为某个可接受的值(例如10分钟),并在再次挂起时检查日志,如果它始终与它失败的行相同。

#2


I ran into this problem recently for those who are looking for an answer. I had task scheduler calling the .php file directly. It would hang and never return anything.

我最近遇到了那些正在寻找答案的人遇到这个问题。我让任务调度程序直接调用.php文件。它会挂起,永远不会返回任何东西。

I found that running firefox.exe and sending the url as a parameter was the proper way to run a .php file through task manager.

我发现运行firefox.exe并将url作为参数发送是通过任务管理器运行.php文件的正确方法。

firefox.exe "http://host/dir/file.php"