简体   繁体   English

使用wget和Windows Scheduler运行php脚本

[英]Running a php script using wget and Windows Scheduler

On my laptop, I have installed a webserver and have written a script. 在我的笔记本电脑上,我已经安装了网络服务器并编写了脚本。 Accessing the script through http://localhost/test/search.php in any browser makes it run properly: it searches for, downloads and stores certain tweets. 在任何浏览器中通过http://localhost/test/search.php访问该脚本都可以使其正常运行:它搜索,下载并存储某些推文。

I now want to make the script run automagically every 20 minutes, so I downloaded and installed wget for Windows in C:\\Program Files\\GnuWin32\\bin\\wget.exe . 现在,我想使脚本每20分钟自动运行一次,因此我在C:\\Program Files\\GnuWin32\\bin\\wget.exe下载并安装了Windows的wget I then created a Windows Task with the following Action (filled in on the Actions tab): 然后,我使用以下操作(在“操作”选项卡上填写)创建了Windows任务:

Program/script:
"C:\Program Files\GnuWin32\bin\wget.exe"

Add arguments (optional): -O - -q -t 1 http://localhost/test/search.php

As expected, every 20 minutes a command line opens to run the script. 如预期的那样,每20分钟就会打开一个命令行来运行脚本。 However, nothing happens. 但是,什么也没有发生。 The script isn't initialized. 脚本未初始化。 I am sure all my paths are correct. 我确信我所有的路径都是正确的。 What could be causing that the script doesn't get executed? 是什么原因导致脚本无法执行? The command line disappears in a matter of milliseconds, so I cannot see what it says... 命令行在几毫秒内消失,所以我看不到它的意思...

You mention two path to search.php : one is with /test , the other is with /ting . 您提到了search.php两条路径:一个是/test ,另一个是/ting Make sure you are using the same in the browser and wget. 确保在浏览器和wget中使用相同的名称。

If you request a page that does not exist, you will get no error if you supply -q . 如果您请求的页面不存在,则提供-q不会出错。 Replace it with -v or --verbose to see what is wrong. 将其替换为-v--verbose可以查看出了什么问题。

c:\gnuwin32\bin\wget.exe -O - --verbose -t 1 http://localhost/test/search.php
--2013-02-25 23:42:03--  http://localhost/test/search.php
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2013-02-25 23:42:03 ERROR 404: Not Found.

When you get the command line working, you can focus on running it with the task scheduler. 当命令行正常工作时,您可以专注于使用任务计划程序运行它。

You can run the task as yourself, or any other accounts. 您可以自己或任何其他帐户运行任务。 Using LOCAL SERVICE allows you to run it without providing a password, even if you are logged out. 即使您已注销,使用“ LOCAL SERVICE也可以在不提供密码的情况下运行它。 Just make shure that the account you selected has permission to the directory where wget is located. 只需确保您选择的帐户具有wget所在目录的权限即可。

Others have mentionned that you must set the path of your scheduled task, or else you will get the operationnal code (2) you showed in your screen shot. 其他人提到您必须设置计划任务的路径,否则您将获得屏幕快照中显示的操作代码(2)。 It will also make sure WIndows find wget.exe dependencies, namely libeay32.dll and libssl32.dll . 它还将确保Windows找到wget.exe依赖项,即libeay32.dlllibssl32.dll

为计划的任务设置工作目录 .

Finally, simplify. 最后,简化。 Run php from the command line. 从命令行运行php。 The link above is an example of that with a simple script. 上面的链接是一个带有简单脚本的示例

Write you code so that you can call search.php standalone from the command line. 编写代码,以便您可以从命令行独立调用search.php。 No need to put an HTTP client and a web server in between. 无需在两者之间放置HTTP客户端和Web服务器。 Here are a bunch of sample scripts for various Twitter related operations. 这是用于各种Twitter相关操作的一堆示例脚本

Please create a bat file, mention all commands there, like the one mentioned by Broncha in the comment & then add it to the Windows task scheduler. 请创建一个bat文件,在其中提及所有命令,例如Br​​oncha在注释中提到的命令,然后将其添加到Windows任务计划程序中。

task1.bat
-----------
cd c:\xampp\htdocs\test
php search.php

You should test your script from command line to know if it is working or not. 您应该从命令行测试脚本,以了解脚本是否正常运行。 Simply run this on cmd to check 只需在cmd上运行此cmd即可检查

"C:\Program Files\GnuWin32\bin\wget.exe" -O - -q -t 1 http://localhost/test/search.php

Task scheduler has lots of tabs for settings, check history tab to see scheduled task history, where you can check the exit code for last several runtimes. 任务计划程序具有许多用于设置的选项卡,请检查历史记录选项卡以查看计划的任务历史记录,您可以在其中检查最后几个运行时的退出代码。 Look for events like this in the history : 在历史中查找类似事件:

Event ID : 201 Task Category : Action Completed 事件ID:201任务类别:操作已完成

Check its exit code action "C:\\Program Files (x86)\\GnuWin32\\bin\\wget.exe" with return code x . action "C:\\Program Files (x86)\\GnuWin32\\bin\\wget.exe" with return code x检查其退出代码action "C:\\Program Files (x86)\\GnuWin32\\bin\\wget.exe" with return code x If it is zero then it is working, any other integer means error. 如果它为零,那么它正在工作,任何其他整数都意味着错误。

Please tell which windows are you using ? 请告诉您您正在使用哪个窗户? Also post the output of the command mentioned above so that I can know exactly what is the error in Task Scheduler. 还要发布上述命令的输出,以便我可以确切地知道Task Scheduler中的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM