在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:07:47  来源:igfitidea点击:

Run Cron Job on PHP Script, on localhost in Windows

phpwindowscronlocalhostwamp

提问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.phpevery 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

  1. Open Task Schedulerfrom windows Startmenu
  2. Go to Actionmenu and hit Create Task...
  3. in Generaltab, fill the Nameand Descriptionfields as you want
  4. in Triggerstab, hit Newbutton.
  5. from Begin the Taskdropdown, select On a scheduleand choose Daily
  6. from Advanced settingssection, select Repeat task everyas you want and set for a durationon Indefinitely.
  7. on Actionstab, from Actiondropdown, select Start a program.
  8. on the Program\scriptbox, enter path to shellscript.vbs like C:\path\to\shellscript.vbs.
  9. leave Add argumentts (optional)section empty.
  10. in Start in (optional)box, enter parent directory of shellscript.vbs like C:\path\to\.
  11. Hit upvote on this tutorial :) Have fun.
  1. 从 Windows开始菜单打开任务计划程序
  2. 转到操作菜单并点击创建任务...
  3. 在“常规”选项卡中,根据需要填写“名称”和“说明”字段
  4. 触发器选项卡中,点击新建按钮。
  5. 从“开始任务”下拉列表中,选择“按计划”并选择“每日”
  6. 从“高级设置”部分,根据需要选择“重复任务”,并将持续时间设置无限期”
  7. 在“操作”选项卡上,从“操作”下拉列表中选择“启动程序”
  8. Program\script框中,输入 shellscript.vbs 的路径,如C:\path\to\shellscript.vbs
  9. 添加参数(可选)部分留空。
  10. Start in (optional)框中,输入 shellscript.vbs 的父目录,如C:\path\to\.
  11. 在本教程中点赞 :) 玩得开心。

回答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 中应该类似。

https://www.drupal.org/node/31506

https://www.drupal.org/node/31506

回答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:

你应该使用这个:

  1. Instead "enter path to shellscript.vbs like C:\path\to\shellscript.vbs." use "C:\Windows\System32\wscript.exe"
  2. Instead "leave Add argumentts (optional) section empty" use (with quotes): "C:\path\to\shellscript.vbs"
  1. 而是“输入 shellscript.vbs 的路径,如 C:\path\to\shellscript.vbs。” 使用“C:\Windows\System32\wscript.exe”
  2. 相反,“将添加参数(可选)部分留空”使用(带引号):“C:\path\to\shellscript.vbs”