在 Windows 中使用 php 设置 cron 作业 [英] Use php to set cron jobs in Windows

查看:27
本文介绍了在 Windows 中使用 php 设置 cron 作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用 PHP 设置 cron 作业的方法.我想要做的就是在特定时间运行 PHP 脚本.用户首先在脚本中输入时间,服务器根据指定的时间运行脚本.我使用的是 Windows 7 和 xampp.

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.

我发现的是:

  1. 创建一个调用 cron.php 文件的 php 文件:使用记事本(或其他),将以下内容粘贴到一个新文件中:$data = file(http://pearl.supplychain.com/cron.php");您需要将它放在常规的 php 标签中,并带有小于号"?php 在前面,而 ​​?最后大于符号".(我似乎不能直接输入,因为它是可疑内容",而 Drupal 不允许)将它保存为executecron.php,与cron.php (htdocs) 同目录.

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

  1. 打开开始-所有程序-附件-系统工具-计划任务.
  2. 双击计划任务.
  3. 设置一个每日任务,从上午 12:00 开始,每半小时(或其他时间)运行一次,直到晚上 11:59.告诉任务运行"以下内容:

  1. Open Start–All Programs–Accessories–System tools–Scheduled tasks.
  2. Double-click on scheduled tasks.
  3. 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:cmsxamppphpphp.exe c:cmsxampphtdocsexecutecron.php

(在本系统中,php.exe 安装在 C:cmsxamppphp,但您可能需要更改路径).

(On this system, php.exe is installed in C:cmsxamppphp, but you’ll probably have to change the path).

如您所见,要做到这一点,必须打开开始-所有程序-附件-系统工具-计划任务.

As you can see, to do this, one must Open Start–All Programs–Accessories–System tools–Scheduled tasks.

它可以通过 php 代码或使用其他方式来实现吗?因为我希望在 php/server 上完成所有工作,而不是需要我的用户自己配置 cron 作业.这意味着我希望我的 php 代码可以在服务器中设置 cron 并且服务器会查看 cron?

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?

致 stefgosselin:

创建批处理文件

Open Notepad.
Paste the line "C:xamppphpphp.exe C:wampwwwindex.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.

我可以使用 php 创建一个 Bath 文件,但是,有什么方法可以粘贴以下schtasks/create/sc minute/mo 20/tn "PHP Cron Job"/tr C:cron.bat使用 php 而不是使用 os?谢谢

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

谢谢

推荐答案

我在 waytocode.com 上找到了这个问题的答案

I found my answer to that question at waytocode.com

他们提供了 3 种可能的解决方案来在 Windows 上运行 cron 作业:

They provide 3 possible solutions to run cron jobs on Windows:

Solution-1 使用任务调度器

在您的 Windows 7/windows 2005/2008 中.

In your Windows 7/windows 2005/2008.

转到开始菜单->所有程序->附件->系统工具->任务计划程序->创建任务

Go to Startmenu->All Programs->Accessories->System Tools->Task Scheduler->create Task

在新窗口中:

  1. 常规(给出任务名称,为了进行测试,您可以选择用户登录时运行")

  1. 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". )

操作(这是最重要的部分.选择 Mozilla firefox 作为程序/脚本",并在参数中提供使用 Mozilla firefox 触发的 URL.

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

解决方案 2 从您的 XAMPP 服务器使用任务调度程序和 PHP

Solution-2 using Task scheduler and PHP from your XAMPP server

在Windows Xp中,无需复制或安装任何东西(已经像XAMPP一样在服务器上安装了PHP)

In Windows Xp,no need to copy or install anything(Already PHP is installed on the server like XAMPP)

转到任务调度程序

创建一个任务给运行时间,然后在运行"命令文本框类型的高级设置选项中

Create a task give Running time, then in avanced setting option in the "RUN" command textbox type

C:xamppphpphp.exe -f c:/xampp/htdocs/waytocode/mycron.php

在 Windows 7/server 2005/2008 中

In Windows 7/server 2005/2008

无需复制或安装任何东西(服务器上已经安装了PHP)

No need to copy or install anything(Already PHP is installed on the server)

在触发器设置中创建一个任务给运行时间.然后在程序/脚本"命令文本框类型的动作设置选项中

Create a task give Running time in Trigger setting.Then in Action setting option in the "Program/Script" command textbox type

C:xamppphpphp.exe

并在添加参数(可选)"中输入

and in the "Add arguments (optional)" type

-f c:/xampp/htdocs/mycron.php

解决方案-3安装一个Windows exe文件,该文件将模拟*nix系统中的cron作业

Solution–3 install a Windows exe file that will simulate the cron job from *nix system

我不喜欢在我的服务器或开发机器上安装任何 exe 文件,但我会在他们发布时提供解决方案:

I don't like to install any exe file to my servers or development machine, but I'll provide the solution as they posted:

在 Windows Xp 中,使用 wget.exe 将所有 2 个 DLL 文件复制到 C:windows 文件夹

In Windows Xp,Copy all 2 DLL file with wget.exe to the C:windows folder

创建一个任务给运行时间,然后在运行"命令文本框类型的高级设置选项中

Create a task give Running time then in avanced setting option in the "RUN" command textbox type

C:Windowswget.exe -q -O NUL http://localhost/mycron.php

在 Windows 7/server 2005/2008 中,使用 wget.exe 将所有 2 个 DLL 文件复制到 C:windows 文件夹

In Windows 7/server 2005/2008 ,Copy all 2 DLL file with wget.exe to the C:windows folder

创建一个任务给运行时间,然后在程序/脚本"命令文本框类型的高级设置选项中

Create a task give Running time then in avanced setting option in the "Program/Script" command textbox type

C:Windowswget.exe

并在添加参数(可选)"中输入

and in the "Add arguments (optional)" type

-q -O NUL http://localhost/mycron.php

Solution-4 使用 .bat 文件和任务调度程序

Solution-4 using a .bat file and the task scheduler

我在 Stackoverflow 上找到了它,它类似于前两个:

I found it here at Stackoverflow and it is similar to the first 2:

  1. 创建一个cron.php文件(你要定期执行的代码)

  1. Create a cron.php file (the code you want to execute at a regular interval)

创建一个 CRON.BAT 文件,将下面的代码复制并粘贴到文件中

Create a CRON.BAT file, copy and past the below code in the file

D:xamppphpphp.exe D:xampphtdocsApplicationcron.php

D:xamppphpphp.exe D:xampphtdocsApplicationcron.php

我写的路径是根据我的xampp和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

  1. 安排任务单击开始 > 所有程序 > 附件 > 系统工具 > 计划任务

或者你可以直接去控制面板 > 计划任务

Or you can go directly Control Panel > Scheduled Tasks

在文件夹中右击新建 > 计划任务

Right click in the folder New > Schedule Task

为任务指定适当的名称.在 RUN 文本字段中...输入 CRON.BAT 文件的完整路径在我的情况下是

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

D:xampphtdocsApplicationCRON.BAT

设置作业的时间表,如果需要,您可以使用高级按钮.

Set the schedule of the job, you can use advanced button if required.

解决方案 5

我也不喜欢它,因为一个脚本不能依赖于其他网站,但无论如何它是一种解决方案.

I don't like it either because one script can't depend on someone else website but it is a solution anyway.

使用外部在线 cron 作业服务.

Use an external online cron job service.

https://www.google.ca/search?q=cron+job+online+service

选择一种更适合您的解决方案.希望这会对某人有所帮助.

更新

Solution-6(基于以下答案,也适用于 CodeIgniter!)

Solution-6 (Based on the answers below, and works with CodeIgniter too!)

创建cron.bat文件并写入以下命令并保存.

Create the cron.bat file and write the following command and save it.

@ECHO OFF
c:
cd C:Program FilesInternet 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:xampppathhtdocsfolderincludescron.bat

结束更新

回答您的问题:

它可以通过 php 代码或使用其他方式来实现吗?因为我想要在 php/服务器上完成所有工作,而不是需要我的用户配置cron 工作本身.这意味着我希望我的 php 代码可以设置服务器中的 cron,服务器会查看 cron?

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?

还有其他方法可以做到这一点:

There are other ways to do this:

在 PHP 中使用 cron 管理器在 PHP 中使用 cron 管理器

Using cron manager from within PHP Using cron manager from within PHP

使用 PHP 管理 Cron 作业http://code.tutsplus.com/教程/managing-cron-jobs-with-php-2--net-19428

Managing Cron Jobs with PHP http://code.tutsplus.com/tutorials/managing-cron-jobs-with-php-2--net-19428

不幸的是,所有使用 PHP 的解决方案都需要 *nix 服务器类型和/或 cPanel,并且实施起来或多或少复杂.

Unfortunately, all solutions with PHP needs a *nix server type and / or cPanel and are more or less complicated to implement.

这篇关于在 Windows 中使用 php 设置 cron 作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆