如何使用 Windows 计划任务运行 PHP 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4701861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I run a PHP script using windows schedule task?
提问by aron n
I have installed localhost/server in my machine and I need to run a php script using windows schedule task. how do I add path in "Actions" tab in schedule task / cofigure the script to run for particular period?
我已经在我的机器上安装了 localhost/server,我需要使用 Windows 计划任务运行一个 php 脚本。如何在计划任务的“操作”选项卡中添加路径/配置脚本以在特定时间段运行?
回答by Pekka
Locate the php.exe executable on your system and pass it the name of the script file using the -f
parameter.
在您的系统上找到 php.exe 可执行文件,并使用-f
参数将脚本文件的名称传递给它。
Example:
例子:
C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php
Reference:
参考:
回答by aron n
Here's how did it.
这是如何做到的。
Windows scheduler -> create a new task -> action tab -> Edit
Windows 调度程序 -> 创建新任务 -> 操作选项卡 -> 编辑
回答by Eaint
At least I tried out some suggestions but it doesn't work so I tried this.
至少我尝试了一些建议但它不起作用所以我尝试了这个。
Use a bat file and schedule to execute that bat file.
使用 bat 文件并计划执行该 bat 文件。
For example in the bat file executephp.bat, write this
比如在bat文件executephp.bat中,写这个
c:\xampp\php\php.exe -f c:\xampp\htdocs\do_something.php
c:\xampp\php\php.exe -fc:\xampp\htdocs\do_something.php
save that bat file that contains that line.
保存包含该行的 bat 文件。
Go to windows scheduler and create a new task and in action tab, browse to point that executephp.bat and for start in -> direct to the directory u have that executephp.bat.
转到 Windows 调度程序并创建一个新任务并在操作选项卡中,浏览以指向该 executephp.bat 并开始 -> 直接到您拥有该 executephp.bat 的目录。
For example if u save the file under C:\xampp\htdocs put that C:\xampp\htdocs in the start in.
例如,如果您将文件保存在 C:\xampp\htdocs 下,请将 C:\xampp\htdocs 放在开头。
Remember to invoke the script even when the user is not logged on.
即使用户未登录,请记住调用脚本。
Everything is set and it will execute without problem.
一切都已设置,它将毫无问题地执行。
回答by Brad Christie
You can use PHP Command Lineto execute it rather then trying to load it through the browser.
您可以使用PHP 命令行来执行它,而不是尝试通过浏览器加载它。
Under the actions tab, create a new action and:
在操作选项卡下,创建一个新操作并:
- Program/Script: Point to your PHP.exe file
- Add Arguments:
-f /path/to/php/file.php
- 程序/脚本:指向您的 PHP.exe 文件
- 添加参数:
-f /path/to/php/file.php
Optionally you can make it start in the script's directory as well.
或者,您也可以使其在脚本目录中启动。
回答by Matt G
I just wanted to leave what i had to do to get this working for server 2012. Which was what has previously been said but with added quotes and using the 'Add arguments' box. So in Task Scheduler->Actions->Edit Action.
我只是想留下我必须做的事情才能让服务器 2012 正常工作。这是之前所说的,但添加了引号并使用了“添加参数”框。所以在Task Scheduler->Actions->Edit Action。
Program/script: "C:\xampp\php\php.exe"
Add arguments: -f "<full path and filename>"
Start in: <Path to file>
(I had to use 'start in' as i referred to class's within the script)
(我必须使用“开始”,因为我在脚本中提到了类)
Hope this helps.
希望这可以帮助。
回答by Justin
Here's how I did it.
这是我如何做到的。
In the Run box: c:\location_of_my_php_installation\php.exe -f c:\location_of_my_php_file\php_file.php
在运行框中: c:\location_of_my_php_installation\php.exe -f c:\location_of_my_php_file\php_file.php
In the Start in box: c:\location_of_my_php_installation\php.exe
在开始于框中: c:\location_of_my_php_installation\php.exe
回答by mahendra Kumar
create Schedule task Scheduler->Actions->Edit Action
创建计划任务 Scheduler->Actions->Edit Action
if you have php file
如果你有php文件
Program/script: powershell
Add arguments: curl http://localhost/demo/cron.php
if you have MVC/CMS URL
如果你有 MVC/CMS URL
Program/script: powershell
Add arguments: curl http://localhost/demo/controller/method
回答by Bobby Etheredge
If the answer given by Pekkadoes not work (C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php
), make sure that you have the correct PHP extensions enabled as well as the correct php.ini
file being used for the PHP version you are using.
如果Pekka 给出的答案不起作用 ( C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php
),请确保您启用了正确的 PHP 扩展以及php.ini
用于您正在使用的 PHP 版本的正确文件。
I ran into this issue recently and resolved it. I was using PHP v5.4 to run my script.php that was nested within the top level PHP folder (v5.3). When I ran the script.php
, from within the v5.4 folder, it was using the v5.3 php.ini
file with different extensions, which caused the script.php
to fail.
我最近遇到了这个问题并解决了它。我使用 PHP v5.4 运行嵌套在顶级 PHP 文件夹 (v5.3) 中的 script.php。当我script.php
从 v5.4 文件夹中运行 , 时,它使用的是php.ini
具有不同扩展名的 v5.3文件,这导致了script.php
失败。
To fix this, here is what I did within the Task Scheduler : Actions tab
为了解决这个问题,这是我在任务计划程序中所做的:操作选项卡
Program Script:
程序脚本:
"C:\Program Files (x86)\PHP\v5.4\php.exe"
"C:\Program Files (x86)\PHP\v5.4\php.exe"
Add Arguements:
添加参数:
-c "C:\Program Files (x86)\PHP\v5.4\php.ini" -f "C:\Xampp\htdocs\script.php"
-c "C:\Program Files (x86)\PHP\v5.4\php.ini" -f "C:\Xampp\htdocs\script.php"
Using the -c
option, you can specify which php.ini
file should be used (stackflow answer).
使用该-c
选项,您可以指定php.ini
应该使用哪个文件(stackflow answer)。
I verified which php was in use by following this stackflow answer.
我按照这个stackflow answer验证了正在使用哪个 php 。
In command line, type php -m
to check which extensions are enabled.
Then, use php --ini
to check which .ini
file(s) is/are being read by PHP.
在命令行中,键入php -m
以检查启用了哪些扩展。然后,用于php --ini
检查.ini
PHP 正在读取哪些文件。
You may not be using the correct ini file for the PHP version you are using if you have multiple versions installed.
如果您安装了多个版本,则您可能没有为您正在使用的 PHP 版本使用正确的 ini 文件。
回答by Riyas PK
you can directly call your local host url by using
您可以通过使用直接调用您的本地主机网址
explorer "http://localhost/yourFile.php"
回答by Sagar Jain
To create a scheduler in Windows, you need to:
要在 Windows 中创建调度程序,您需要:
- Create
.bat
file on your server or system; - Type the following command in your
.bat
file:“F:\xampp\php\php.exe” -f “F:/xampp/htdocs/sitefolder/test.php”;
- Set the scheduler time and file in your task scheduler in Windows.
.bat
在您的服务器或系统上创建文件;- 在您的
.bat
文件中键入以下命令:“F:\xampp\php\php.exe” -f “F:/xampp/htdocs/sitefolder/test.php”;
- 在 Windows 的任务计划程序中设置计划程序时间和文件。