在 Windows 本地主机上的 PHP 脚本上运行 Cron 作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24035090/
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
Run Cron Job on PHP Script, on localhost in Windows
提问by Pmpr
I have a php script and want to run it on an schedule. I am using local web server on windows (WAMP server) and need a way to run my_script.php
every 10 min.
我有一个 php 脚本,想按计划运行它。我在 Windows 上使用本地 Web 服务器(WAMP 服务器)并且需要一种my_script.php
每 10 分钟运行一次的方法。
How to run a cron job on a PHP script, on localhost in windows?
如何在 Windows 本地主机上的 PHP 脚本上运行 cron 作业?
回答by Pmpr
recently I had sort of problems to run a cron job on a php script on localhost (WAMP server) in windows 7, when I was on a test to chronically fetch some links from www out there.
最近,我在 Windows 7 中的本地主机(WAMP 服务器)上的 php 脚本上运行 cron 作业时遇到了一些问题,当时我正在测试从 www 那里长期获取一些链接。
By the way I am sharing this for anyone that is on the same thing.
顺便说一下,我正在为任何有同样事情的人分享这个。
You will need a shellscriptto run chronically, using Windows Task Scheduler. Also you will need a batch script (script.bat) to call the php.exeand run your php script(here called as my_process.php
)
您将需要一个shellscript来长期运行,使用Windows 任务计划程序。此外,您还需要一个批处理脚本(script.bat)来调用php.exe并运行您的php 脚本(此处称为my_process.php
)
shellscript.vbs
shellscript.vbs
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\path\to\script\script.bat" & Chr(34), 0
Set WinScriptHost = Nothing
script.bat
脚本文件
"C:\wamp\bin\php\php5.4.12\php.exe" -f "C:\wamp\www\website\my_process.php"
Now, we are ready to set the Windows Task Schedulerto run shellscript.vbsat the required time interval:
现在,我们准备设置Windows 任务计划程序以在所需的时间间隔运行shellscript.vbs:
- Open Task Schedulerfrom windows Startmenu
- Go to Actionmenu and hit Create Task...
- in Generaltab, fill the Nameand Descriptionfields as you want
- in Triggerstab, hit Newbutton.
- from Begin the Taskdropdown, select On a scheduleand choose Daily
- from Advanced settingssection, select Repeat task everyas you want and set for a durationon Indefinitely.
- on Actionstab, from Actiondropdown, select Start a program.
- on the Program\scriptbox, enter path to shellscript.vbs like C:\path\to\shellscript.vbs.
- leave Add argumentts (optional)section empty.
- in Start in (optional)box, enter parent directory of shellscript.vbs like
C:\path\to\
. - Hit upvote on this tutorial :) Have fun.
- 从 Windows开始菜单打开任务计划程序
- 转到操作菜单并点击创建任务...
- 在“常规”选项卡中,根据需要填写“名称”和“说明”字段
- 在触发器选项卡中,点击新建按钮。
- 从“开始任务”下拉列表中,选择“按计划”并选择“每日”
- 从“高级设置”部分,根据需要选择“重复任务”,并将持续时间设置为“无限期”。
- 在“操作”选项卡上,从“操作”下拉列表中选择“启动程序”。
- 在Program\script框中,输入 shellscript.vbs 的路径,如C:\path\to\shellscript.vbs。
- 将添加参数(可选)部分留空。
- 在Start in (optional)框中,输入 shellscript.vbs 的父目录,如
C:\path\to\
. - 在本教程中点赞 :) 玩得开心。
回答by Ravi Chauhan
To setup a Windows machine to run cron.php at a specific time follow the specific instructions below. This can be useful if you are not familiar with Linux/Unix, or if your web host does not offer the ability to run cron jobs; you can run them remotely from your own computer.
要设置 Windows 机器在特定时间运行 cron.php,请遵循以下特定说明。如果您不熟悉 Linux/Unix,或者您的 Web 主机不提供运行 cron 作业的能力,这会很有用;您可以从自己的计算机远程运行它们。
Note: These instructions were written for Windows XP but should be similar in other versions of Windows.
注意:这些说明是为 Windows XP 编写的,但在其他版本的 Windows 中应该类似。
回答by DimaX
If you use answer from Trixand get same problem as Metafaniel:
如果您使用Trix 的答案并遇到与Metafaniel相同的问题:
I have a problem, with this procedure, the task is beinge executed, however my php script it's not running, I got the "Open with" dialogue. If I see the properties of my task, it states: ActionName C:\Windows\system32\OpenWith.exe even if I set it to the vbs file as you suggested. What am I doing wrong?
我有一个问题,在这个过程中,任务正在执行,但是我的 php 脚本没有运行,我得到了“打开方式”对话框。如果我看到我的任务的属性,它会指出: ActionName C:\Windows\system32\OpenWith.exe 即使我按照您的建议将其设置为 vbs 文件。我究竟做错了什么?
you should use this:
你应该使用这个:
- Instead "enter path to shellscript.vbs like C:\path\to\shellscript.vbs." use "C:\Windows\System32\wscript.exe"
- Instead "leave Add argumentts (optional) section empty" use (with quotes): "C:\path\to\shellscript.vbs"
- 而是“输入 shellscript.vbs 的路径,如 C:\path\to\shellscript.vbs。” 使用“C:\Windows\System32\wscript.exe”
- 相反,“将添加参数(可选)部分留空”使用(带引号):“C:\path\to\shellscript.vbs”