Running a php script using wget and Windows Schedu

2020-07-26 04:52发布

问题:

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.

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. I then created a Windows Task with the following Action (filled in on the Actions tab):

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. 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...

回答1:

You mention two path to search.php : one is with /test, the other is with /ting. Make sure you are using the same in the browser and wget.

If you request a page that does not exist, you will get no error if you supply -q. Replace it with -v or --verbose to see what is wrong.

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. Just make shure that the account you selected has permission to the directory where wget is located.

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. It will also make sure WIndows find wget.exe dependencies, namely libeay32.dll and libssl32.dll.

.

Finally, simplify. Run php from the command line. 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. No need to put an HTTP client and a web server in between. Here are a bunch of sample scripts for various Twitter related operations.



回答2:

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.

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


回答3:

You should test your script from command line to know if it is working or not. Simply run this on cmd to check

"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

Check its exit code 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.