在 Windows 中使用 php 设置 cron 作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9894804/
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
Use php to set cron jobs in Windows
提问by Leo Chan
I am looking for a way to set cron job using PHP. All I would like to do is run a PHP script at a specific time. The user first inputs a time in a script, according to the time specified the server will run the script. I am using windows 7 and xampp.
我正在寻找一种使用 PHP 设置 cron 作业的方法。我想要做的就是在特定时间运行 PHP 脚本。用户首先在脚本中输入时间,服务器根据指定的时间运行脚本。我正在使用 Windows 7 和 xampp。
What I have found is:
我发现的是:
Create a php file that calls the cron.php file: Using notepad (or whatever), paste the following into a new file: $data = file(“http://pearl.supplychain.com/cron.php”); you'll need to put it inside the regular php tags, with the “less than sign” ? php at the front, and the ? “greater than sign” at the end. (I can't seem to just type that because it is “suspicious content” and drupal doesn't allow it) Save it as executecron.php, into the same directory as cron.php (htdocs).
Set up a scheduled task that calls this regularly:
- Open Start–All Programs–Accessories–System tools–Scheduled tasks.
- Double-click on scheduled tasks.
Set up a Daily task that starts at 12:00 am and runs every half hour (or whatever) until 11:59 pm. Tell the task to “run” the following:
C:\cms\xampp\php\php.exe c:\cms\xampp\htdocs\executecron.php
(On this system, php.exe is installed in C:\cms\xampp\php, but you'll probably have to change the path).
创建一个调用 cron.php 文件的 php 文件:使用记事本(或其他),将以下内容粘贴到一个新文件中: $data = file(“http://pearl.supplychain.com/cron.php”); 您需要将它放在常规的 php 标签中,并带有“小于号”?php 在前面,而 ? 最后“大于符号”。(我似乎不能只输入它,因为它是“可疑内容”,并且 drupal 不允许)将其另存为 executecron.php,与 cron.php (htdocs) 位于同一目录中。
设置一个定期调用它的计划任务:
- 打开开始——所有程序——附件——系统工具——计划任务。
- 双击计划任务。
设置一个每日任务,从上午 12:00 开始,每半小时(或其他时间)运行一次,直到晚上 11:59。告诉任务“运行”以下内容:
C:\cms\xampp\php\php.exe c:\cms\xampp\htdocs\executecron.php
(在此系统上,php.exe 安装在 C:\cms\xampp\php 中,但您可能需要更改路径)。
As you can see, to do this, one must Open Start–All Programs–Accessories–System tools–Scheduled tasks.
如您所见,要做到这一点,必须 Open Start–All Programs–Accessories–System tools–Scheduled tasks.
Can it specific by php code or using another way to do this? Because i want all the work done on php / server instead of need my user config the cron job themselves. Which means i want my php code can set the cron in server and server will look at the cron?
它可以通过 php 代码或使用另一种方式来实现吗?因为我希望在 php/server 上完成所有工作,而不是需要我的用户自己配置 cron 作业。这意味着我希望我的 php 代码可以在服务器中设置 cron 并且服务器会查看 cron?
To stefgosselin:
斯特夫戈瑟林:
To create the batch file
Open Notepad. Paste the line "C:\xampp\php\php.exe C:\wamp\www\index.php" Click "File" -> "Save As" Ensure "Save as type:" is set to "All Files" Save the file as "cron.bat" to your C drive
To schedule the batch file to run
Open Command Prompt Paste the following "schtasks /create /sc minute /mo 20 /tn "PHP Cron Job" /tr C:\cron.bat" Press Enter This will make the script run every 20 minutes, the first time 20 minutes from now.
创建批处理文件
Open Notepad. Paste the line "C:\xampp\php\php.exe C:\wamp\www\index.php" Click "File" -> "Save As" Ensure "Save as type:" is set to "All Files" Save the file as "cron.bat" to your C drive
安排批处理文件运行
Open Command Prompt Paste the following "schtasks /create /sc minute /mo 20 /tn "PHP Cron Job" /tr C:\cron.bat" Press Enter This will make the script run every 20 minutes, the first time 20 minutes from now.
I am able to create a bath file using php, however, are there any way to Paste the following "schtasks /create /sc minute /mo 20 /tn "PHP Cron Job" /tr C:\cron.bat
using php instead of using os? Thank you
我可以使用 php 创建一个 Bath 文件,但是,有什么方法可以Paste the following "schtasks /create /sc minute /mo 20 /tn "PHP Cron Job" /tr C:\cron.bat
使用 php 而不是使用 os 吗?谢谢
Thank you
谢谢
回答by Adrian P.
I found my answer to that question at waytocode.com
我在waytocode.com 上找到了这个问题的答案
They provide 3 possible solutions to run cron jobs on Windows:
它们提供了 3 种可能的解决方案来在 Windows 上运行 cron 作业:
Solution-1using Task scheduler
解决方案 1使用任务调度程序
In your Windows 7/windows 2005/2008.
在您的 Windows 7/Windows 2005/2008 中。
Go to Startmenu->All Programs->Accessories->System Tools->Task Scheduler->create Task
转到开始菜单->所有程序->附件->系统工具->任务计划程序->创建任务
In the new window:
在新窗口中:
General (Give the Task name and for testing you can select “Run when User is logged in“)
Trigger (You can Select the running interval as “daily,weekly,monthly”. )
Action (This is most important part. Select a Mozilla firefox as the “program/script” and in the Argument provide the URL to fire with Mozilla firefox).
常规(给出任务名称,为了进行测试,您可以选择“用户登录时运行”)
触发器(您可以选择运行间隔为“每天、每周、每月”。)
操作(这是最重要的部分。选择 Mozilla firefox 作为“程序/脚本”,并在参数中提供使用 Mozilla firefox 触发的 URL)。
Solution-2using Task scheduler and PHP from your XAMPP server
解决方案 2使用 XAMPP 服务器中的任务调度程序和 PHP
In Windows Xp,no need to copy or install anything(Already PHP is installed on the server like XAMPP)
在 Windows Xp 中,无需复制或安装任何东西(已经像 XAMPP 一样在服务器上安装了 PHP)
Goto Task scheduler
转到任务调度程序
Create a task give Running time, then in avanced setting option in the “RUN” command textbox type
创建一个任务给运行时间,然后在“运行”命令文本框类型的高级设置选项中
C:\xampp\php\php.exe -f c:/xampp/htdocs/waytocode/mycron.php
In Windows 7/server 2005/2008
在 Windows 7/服务器 2005/2008 中
No need to copy or install anything(Already PHP is installed on the server)
无需复制或安装任何东西(服务器上已经安装了 PHP)
Create a task give Running time in Trigger setting.Then in Action setting option in the “Program/Script” command textbox type
在触发器设置中创建一个任务给运行时间。然后在“程序/脚本”命令文本框类型的动作设置选项中
C:\xampp\php\php.exe
and in the “Add arguments (optional)” type
并在“添加参数(可选)”类型中
-f c:/xampp/htdocs/mycron.php
Solution–3install a Windows exe file that will simulate the cron job from *nix system
解决方案-3安装一个 Windows exe 文件,该文件将模拟 *nix 系统中的 cron 作业
I don't like to install any exe file to my servers or development machine,but I'll provide the solution as they posted:
我不喜欢在我的服务器或开发机器上安装任何 exe 文件,但我会在他们发布时提供解决方案:
In Windows Xp,Copy all 2 DLL file with wget.exe to the C:\windows folder
在 Windows Xp 中,使用 wget.exe 将所有 2 个 DLL 文件复制到 C:\windows 文件夹
Create a task give Running time then in avanced setting option in the “RUN” command textbox type
创建一个任务给运行时间,然后在“运行”命令文本框类型的高级设置选项中
C:\Windows\wget.exe -q -O NUL http://localhost/mycron.php
In Windows 7/server 2005/2008 ,Copy all 2 DLL file with wget.exe to the C:\windows folder
在 Windows 7/server 2005/2008 中,使用 wget.exe 将所有 2 个 DLL 文件复制到 C:\windows 文件夹
Create a task give Running time then in avanced setting option in the “Program/Script” command textbox type
创建一个任务给运行时间,然后在“程序/脚本”命令文本框类型的高级设置选项中
C:\Windows\wget.exe
and in the “Add arguments (optional)” type
并在“添加参数(可选)”类型中
-q -O NUL http://localhost/mycron.php
Solution-4using a .bat file and the task scheduler
解决方案 4使用 .bat 文件和任务调度程序
I found it here at Stackoverflow and it is similar to the first 2:
我在 Stackoverflow 上找到了它,它类似于前两个:
Create a cron.php file (the code you want to execute at a regular interval)
Create a CRON.BAT file, copy and past the below code in the file
D:\xampp\php\php.exe D:\xampp\htdocs\Application\cron.php
创建一个 cron.php 文件(你要定期执行的代码)
创建一个 CRON.BAT 文件,复制并粘贴文件中的以下代码
D:\xampp\php\php.exe D:\xampp\htdocs\Application\cron.php
The path I have written is according to my xampp and cron.php file, update the path of files according to your system directory
我写的路径是根据我的xampp和cron.php文件,根据你的系统目录更新文件的路径
- To schedule a task Click on start > All Programs > Accessories > System Tools > Scheduled Tasks
- 计划任务 单击开始 > 所有程序 > 附件 > 系统工具 > 计划任务
Or you can go directly Control Panel > Scheduled Tasks
或者您可以直接进入控制面板 > 计划任务
Right click in the folder New > Schedule Task
右键单击文件夹新建 > 计划任务
Give appropriate name to the Task. In the RUN text field… Type the complete path of the CRON.BAT file in my case it is
为任务指定适当的名称。在 RUN 文本字段中...输入 CRON.BAT 文件的完整路径,在我的情况下是
D:\xampp\htdocs\Application\CRON.BAT
Set the schedule of the job, you can use advanced button if required.
设置作业的时间表,如果需要,您可以使用高级按钮。
Solution-5
解决方案5
I don't like it either because one script can't depend on someone else website but it is a solution anyway.
我也不喜欢它,因为一个脚本不能依赖于其他网站,但无论如何它都是一种解决方案。
Use an external online cron job service.
使用外部在线 cron 作业服务。
https://www.google.ca/search?q=cron+job+online+service
https://www.google.ca/search?q=cron+job+online+service
Chose one solution that it is more appropriate for you. Hope this will help someone.
选择一种更适合您的解决方案。希望这会帮助某人。
UPDATE
更新
Solution-6(Based on the answers below, and works with CodeIgniter too!)
解决方案 6(基于以下答案,也适用于 CodeIgniter!)
Create the cron.bat file and write the following command and save it.
创建 cron.bat 文件并写入以下命令并保存。
@ECHO OFF
c:
cd C:\Program Files\Internet Explorer
START iexplore.exe http://localhost/path/to/cron/job/1
Create a task give Running time in Trigger setting.Then in Action setting option in the “Program/Script” command textbox type
在触发器设置中创建一个任务给运行时间。然后在“程序/脚本”命令文本框类型的动作设置选项中
C:\xampp\path\htdocs\folder\includes\cron.bat
END UPDATE
结束更新
Answering your question:
回答你的问题:
Can it specific by php code or using another way to do this? Because i want all the work done on php / server instead of need my user config the cron job themselves. Which means i want my php code can set the cron in server and server will look at the cron?
它可以通过 php 代码或使用另一种方式来实现吗?因为我希望在 php/server 上完成所有工作,而不是需要我的用户自己配置 cron 作业。这意味着我希望我的 php 代码可以在服务器中设置 cron 并且服务器会查看 cron?
There are other ways to do this:
还有其他方法可以做到这一点:
Using cron manager from within PHP Using cron manager from within PHP
在 PHP 中使用 cron 管理器在 PHP 中 使用 cron 管理器
Managing Cron Jobs with PHP http://code.tutsplus.com/tutorials/managing-cron-jobs-with-php-2--net-19428
使用 PHP 管理 Cron 作业 http://code.tutsplus.com/tutorials/managing-cron-jobs-with-php-2--net-19428
Unfortunately, all solutions with PHP needs a *nix server type and / or cPanel and are more or less complicated to implement.
不幸的是,所有使用 PHP 的解决方案都需要 *nix 服务器类型和/或 cPanel,并且实现起来或多或少复杂。
回答by stefgosselin
Ok, if I understood correctly, you would like to have a cron job created on a system, without a user having to create the task.
好的,如果我理解正确,您希望在系统上创建一个 cron 作业,而无需用户创建任务。
Basically, this can easily be done in a .bat file, (that could even be called from php). The schtasks app can easily automate the creation of a scheduled task. For example:
基本上,这可以在 .bat 文件中轻松完成(甚至可以从 php 调用)。schtasks 应用程序可以轻松地自动创建计划任务。例如:
schtasks /create /tn UNO /tr YOURAPP.EXE /sc HOURLY /mo 2
See the official MS support pagefor more info on this nifty application.
有关此漂亮应用程序的更多信息,请参阅官方 MS 支持页面。
Another option that can even be easier for the user is to use an installer. I have not created that many windows apps myself but did have the pleasure of playing with NSIS, and this app also has options to create scheduled tasks, among many many other features way too numerous to name here. Highly recommended if you need a user-installable package.
另一个对用户来说更容易的选项是使用安装程序。我自己并没有创建那么多 Windows 应用程序,但确实很高兴使用NSIS,并且这个应用程序还提供了创建计划任务的选项,以及许多其他功能,在此无法一一列举。如果您需要用户可安装的软件包,强烈推荐。
Hope that helps, happy coding friend.
希望有帮助,快乐的编码朋友。
回答by Pierre
Another great tool is available for free on Windows, nncron. It uses the exact same syntax than unix' cron:
另一个很棒的工具是在 Windows 上免费提供的,nncron。它使用与 unix 的 cron 完全相同的语法:
I find it easier to manage that this horrible schtasks :)
我发现管理这个可怕的任务更容易:)
The lite version is the one I would suggest to use. The full powered version supports scripting in the config files. It could be handy but somehow over killed for normal cron jobs.
精简版是我建议使用的版本。完整版本支持在配置文件中编写脚本。它可能很方便,但不知何故对于正常的 cron 工作来说已经过时了。
回答by Pierre
Steps Create Cron in Windows :
在 Windows 中创建 Cron 的步骤:
Step 1> Create .bat file
步骤 1> 创建 .bat 文件
1. open notepad.
2. click File -> Save As.
3.Give the file a name i.e. MyBatchFile.bat Underneath the filename box change the file type to All Files. The .bat on the end of the filename will instruct the PC to open it as a batch file.
Step 2> IN MyBatchFile.bat write following command
步骤 2> 在 MyBatchFile.bat 中写入以下命令
ECHO OFF
START File Full Path write here like http://www.google.com
Step 3> Open Start–All Programs–Accessories–System tools–Scheduled tasks. Click on create task from right side bar. name:any one trigger: set time from drop down and select per day Action:select .bat file Click on OK button
步骤3>打开开始-所有程序-附件-系统工具-计划任务。单击右侧栏中的创建任务。名称:任意一个触发器:从下拉菜单中设置时间并选择每天操作:选择 .bat 文件 单击“确定”按钮
回答by dognose
Another quite simple approach is powershell, to fetch the content of your php-file.
另一个非常简单的方法是 powershell,用于获取 php 文件的内容。
Task-Scheduler, target: Powershell.exe
, use arguments:
任务调度程序,目标:Powershell.exe
,使用参数:
-Command "(New-Object Net.WebClient).DownloadString('http://myhost/cron/cron.php')"
(Ups, old post)
(UPS,旧帖子)
回答by Shraddha Kulkarni
It is the complete solution I have tried for my own project for windows 10.
这是我为自己的Windows 10项目尝试过的完整解决方案。
1) Go to "start" menu and search for "task scheduler" and open it.
1) 转到“开始”菜单并搜索“任务计划程序”并打开它。
If you are facing any issue of permissions, open it as "run as system administrator".This option is exactly below "open".
And still you are facing issues of permissions contact your system admin.
如果您遇到任何权限问题,请将其打开为“以系统管理员身份运行”。此选项正好位于“打开”下方。
您仍然面临权限问题,请联系您的系统管理员。
2) Click on "new task" in right sidebarand a window will be opened.
2) 点击右侧栏中的“新建任务”,会打开一个窗口。
3) In this window "general" tab will be selected by default. Give a suitable name to task and if require provide short description.
3) 在此窗口中,默认选择“常规”选项卡。为任务提供一个合适的名称,如果需要,请提供简短的描述。
4) Go to "triggers" set here frequency at you want to run it and start date-time, if require expires date-time.and set all require parameters.
4)转到“触发器”在这里设置您想要运行它的频率并开始日期时间,如果需要过期日期时间。并设置所有需要参数。
5) Got to "Actions" tab. This is the most important part.
5) 转到“操作”选项卡。这是最重要的部分。
In "Program/script", write the absolute path of "php.exe".
In "Start in", write the absolute path of the folder where your intended php file for cron is.
In "Add arguments", write the name of the php file which you want to get run thought the task job.
In "Add arguments", you can also specify the arguments to the files space separated after php file name. and if it has text or special characters, enclose it with double quotes.
If you provide arguments to scripts, you will receive them in $argv[ 1 ], $argv[ 2 ]and so on respectively.
在“程序/脚本”中,写入“ php.exe”的绝对路径。
在“开始于”中,写下您想要用于 cron 的 php 文件所在文件夹的绝对路径。
在“添加参数”中,写下您希望通过任务作业运行的 php 文件的名称。
在“添加参数”中,您还可以指定参数到php文件名后分隔的文件空间。如果它有文本或特殊字符,用双引号括起来。
如果您为脚本提供参数,您将分别在$argv[1]、$argv[2]等中接收它们。
For more details visit: https://medium.com/@shraddha_kulkarni/run-php-cron-in-windows-513fb1aa53a5
更多详情请访问:https: //medium.com/@shraddha_kulkarni/run-php-cron-in-windows-513fb1aa53a5
回答by Chirag Shah
Steps Create Cron in Windows OS:
在 Windows 操作系统中创建 Cron 的步骤:
Step 1>Create .bat file
步骤 1>创建 .bat 文件
1. Open notepad.
2. Click File -> Save As.
3. Give the file a name i.e. MyBatchFile.bat Underneath the filename box change the file type to All Files. The .bat on the end of the filename will instruct the PC to open it as a batch file.
Step 2> IN MyBatchFile.bat write following command
步骤 2> 在 MyBatchFile.bat 中写入以下命令
ECHO OFF
START File Full Path write here like http://www.google.com
Step 3> Open Start–All Programs–Accessories–System tools–Scheduled tasks.Click on create task from right side bar. name:any one trigger: set time from drop down and select per day Action:select .bat file Click on OK button
步骤3>打开开始-所有程序-附件-系统工具-计划任务。单击右侧栏中的创建任务。名称:任意一个触发器:从下拉菜单中设置时间并选择每天操作:选择 .bat 文件 单击“确定”按钮