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

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

问题描述

我正在寻找一种方式使用PHP设置cron工作。所有我想做的是在特定时间运行PHP脚本。用户首先在脚本中输入时间,根据指定的时间服务器将运行脚本。我使用的是windows 7和xampp。



我发现是:


  1. 创建一个调用cron.php文件的php文件:使用notepad(或其他),将以下内容粘贴到一个新文件中:
    $ data = file(http://pearl.supplychain.com /cron.php);
    你需要把它放在正常的php标签,用小于符号? php在前面, 大于号结束。 (我似乎不能只是键入,因为它是可疑内容和drupal不允许它)
    将它保存为executecron.php,与cron.php(htdocs)相同的目录。


  2. 设置定期调用此任务的计划任务:


    1. - 所有程序 - 附件 - 系统工具 - 计划任务。

    2. 双击计划任务。

    3. 从上午12:00开始,每半小时(或任何时间),直到晚上11:59。告诉任务运行以下内容:

        C:\cms\xampp\php\php.exe c:\cms\xampp\htdocs\executecron.php 

      (在此系统上, php.exe安装在C:\cms\xampp\php中,但您可能必须更改路径)。



您可以看到,为此,必须打开开始 - 所有程序 - 附件 - 系统工具 - 计划任务。



可以通过PHP代码或使用其他方式来执行此操作吗?因为我想要所有的工作完成在PHP /服务器,而不是需要我的用户配置cron作业本身。



到stefgosselin




创建批处理文件

 打开记事本。 
粘贴C:\ xampp\php\php.exe C:\wamp\www\index.php行
单击文件 - > 另存为
确保另存为类型:设置为所有文件
将文件保存为cron.bat到您的C驱动器

计划批处理文件的运行

 命令提示
粘贴以下schtasks / create / sc minute / mo 20 / tnPHP Cron Job/ tr C:\cron.bat
按Enter键
这将使脚本每20分钟运行一次,第一次从现在开始20分钟。




我可以使用php创建浴缸文件,有任何办法粘贴以下schtasks / create / sc分钟/ mo 20 / tnPHP Cron作业/ tr C:\cron.bat
使用php而不是使用os?谢谢



谢谢

解决方案

p>我在waytocode.com找到了我对这个问题的回答



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



解决方案-1 使用任务计划程序



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



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




  1. 一般(提供任务名称,您可以选择当用户登录时运行)


  2. 触发(您可以选择运行时间间隔为每日,每周,每月。)


  3. 行动(这是最重要的部分。选择Mozilla Firefox作为程序/脚本,并在参数中提供用Mozilla firefox启动的URL。


Solution-2 使用任务计划程序和PHP从您的XAMPP服务器



在Windows Xp,无需复制或安装任何东西



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

  C:\ xampp\php\php.exe -fc :/xampp/htdocs/waytocode/mycron.php 

在Windows 7 / server 2005/2008

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



创建任务给运行时间在程序/脚本命令文本框类型中的动作设置选项中。

  C:\ xampp \ php \php.exe 

并在添加参数(可选)类型

  -fc:/xampp/htdocs/mycron.php 

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




,但我会提供他们发布的解决方案:

我不想将任何exe文件安装到我的服务器或开发机器。

在Windows Xp中,将wget.exe的所有DLL文件复制到C:\windows文件夹



创建任务在RUN命令文本框类型中提供运行时间然后在平移设置选项中

  C:\Windows\wget.exe -q -O NUL http://localhost/mycron.php 

在Windows 7 / 2008,Copy all 2 DLL文件与wget.exe到C:\windows文件夹



创建任务给出运行时间然后在avanced设置选项中的程序/脚本命令文本框类型

  C:\Windows \wget.exe 

并在添加参数(可选)类型

中输入

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

解决方案-4 使用.bat文件和任务调度程序



我在Stackoverflow找到它,它类似于前面的2:


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


  2. 创建CRON.BAT文件,复制并覆盖文件中的以下代码



    D:\ xampp\php\php.exe D:\ xampp \htdocs\Application\cron.php


我写的路径是到我的xampp和cron.php文件,根据系统目录更新文件的路径


  1. 计划任务
    点击开始>所有程序>附件>系统工具>预设任务

控制面板>计划任务



右键单击文件夹
新建>计划任务



名称到任务。
在RUN文本字段中输入CRON.BAT文件的完整路径
在我的案例

  D:\ xampp\htdocs\Application\CRON.BAT 

设置



我不喜欢它,因为一个脚本不能依赖于其他网站,但它仍然是一个解决方案。



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



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



选择一个更适合的解决方案您。希望这将有助于某人。



$ b

解决方案-6 (基于下面的答案,也可以使用CodeIgniter)



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

  @ECHO OFF 
c:
cd C:\ Program Files \Internet Explorer
START iexplore.exe http:// localhost / path / to / cron / job / 1

创建一个任务在Trigger setting中设置运行时间。然后在程序/脚本命令文本框类型中设置选项

  C:\xampp\path\htdocs\folder\includes\cron.bat 

结束更新



回答您的问题:


可以通过php代码或使用其他方式来执行此操作吗?因为i
需要所有的工作在php /服务器上完成,而不是需要我的用户配置
cron工作本身。这意味着我想要我的PHP代码可以设置
cron在服务器和服务器将看看cron?


其他方式来执行此操作:



在PHP中使用cron manager


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



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


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.

What I have found is:

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

  2. Set up a scheduled task that calls this regularly:

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

As you can see, to do this, one must 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?

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.

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

Thank you

解决方案

I found my answer to that question at waytocode.com

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

Solution-1 using Task scheduler

In your Windows 7/windows 2005/2008.

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

In the new window:

  1. General (Give the Task name and for testing you can select "Run when User is logged in")

  2. Trigger (You can Select the running interval as "daily,weekly,monthly". )

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

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

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

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

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:\xampp\php\php.exe

and in the "Add arguments (optional)" type

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

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

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

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:\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

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-4 using a .bat file and the task scheduler

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

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

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

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

D:\xampp\htdocs\Application\CRON.BAT

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

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

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

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

@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?

There are other ways to do this:

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

Managing Cron Jobs with PHP 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在Windows中设置cron作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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