php 在 Windows Xampp 中设置 Cronjob

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17442040/
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 12:53:44  来源:igfitidea点击:

Setting Up A Cronjob In Windows Xampp

phpcron

提问by ashley

Help needed to set up this command in my Xampp windows server

在我的 Xampp Windows 服务器中设置此命令所需的帮助

0 * * * *     cd C:/xampp/htdocs/plugins/moviefeed/ && php cron.php

Could you please point me in the right direction

你能指出我正确的方向吗

thanks

谢谢

回答by donald123

On Windows OS there is no cron .... you need to use the scheduler task from Windows to create a "Cronjob". Example for using the windows scheduler

在 Windows 操作系统上没有 cron .... 您需要使用 Windows 的调度程序任务来创建“Cronjob”。使用 Windows 调度程序的示例

回答by oasisfleeting

VladH wrote /st , I believe it should be /sc

VladH 写的是 /st ,我相信它应该是 /sc

Open a command prompt and type

打开命令提示符并键入

schtasks /create /tn "XamppCron" /tr "L:\xampp\php\php.exe L:\xampp\htdocs\mydevsite\cron.php" /sc minute /mo 10

Once you're satisfied with the cron if you run as php-win.exe the command prompt window will not appear everytime the task is run.

一旦您对 cron 感到满意,如果您以 php-win.exe 的身份运行,则不会在每次运行任务时出现命令提示窗口。

回答by VladH

You can easily create a .bat file where you define your schedule task for windows. Regarding your needs..

您可以轻松创建一个 .bat 文件,您可以在其中定义 Windows 的计划任务。关于您的需求..

set doc=C:\xampp\htdocs\project
cd "%doc%"
copy /y nul "file.php"
ECHO ^<?php echo 'This is executed via scheduler task!'; ?^> >file.php
schtasks /create /tn "Cron" /tr "C:\Program Files (x86)\Mozilla Firefox\firefox.exe http://play.local/fisierul.php" /st minute /mo 10

What I did here is:

我在这里做的是:

  • I changed the path to "project" directory from "htdocs"
  • I create a file "file.php" (if it doesn't exists)
  • I write a simple echo into the "file.php" file
  • And I create a new scheduler task (similar to cron jobs in Unix) wich will open my mozilla browser and access that url every 10 minutes.
  • 我将路径从“htdocs”更改为“project”目录
  • 我创建了一个文件“file.php”(如果它不存在)
  • 我在“file.php”文件中写了一个简单的回声
  • 我创建了一个新的调度程序任务(类似于 Unix 中的 cron 作业),它将打开我的 mozilla 浏览器并每 10 分钟访问一次该 url。

Note: to stop a scheduler task you must go in cmd and type

注意:要停止调度程序任务,您必须进入 cmd 并键入

schtasks /delete /tn "Cron"

Good luck dude!

祝你好运!

回答by akinuri

I'll add nothing new but just a test case. Using the Task Scheduler GUI would be troublesome/unnecessary for a simple cron job, so this demo uses .batfiles. What the demo does is just increment the number in the "counter.txt" by 1 every minute.

我不会添加任何新内容,只是一个测试用例。对于简单的 cron 作业,使用 Task Scheduler GUI 会很麻烦/不必要,因此此演示使用.bat文件。演示所做的只是每分钟将“counter.txt”中的数字增加 1。



Created a "cron" folder in "htdocs" with these files:

使用以下文件在“htdocs”中创建了一个“cron”文件夹:

  • counter.txt
  • index.php
  • schtask_add.bat
  • schtask_del.bat
  • schtask_query.bat
  • 计数器.txt
  • 索引.php
  • schtask_add.bat
  • schtask_del.bat
  • schtask_query.bat

Contents of the files:

文件内容:

counter.txt

计数器.txt

0

index.php

索引.php

<?php
    $filepath = "C:/xampp/htdocs/cron/counter.txt";
    $i = file_get_contents($filepath);
    $i = (int) $i;
    $i++;
    file_put_contents($filepath, $i);

schtask_add.bat

schtask_add.bat

@echo off
schtasks /Create /TN XAMPP /TR "C:/xampp/php/php-win.exe C:/xampp/htdocs/cron/index.php" /SC MINUTE /MO 1
pause

schtask_del.bat

schtask_del.bat

@echo off
schtasks /Delete /TN XAMPP /F
pause

schtask_query

schtask_query

@echo off
schtasks /Query /TN XAMPP
pause


Tested with XAMPP 7.1.11 on Windows 10 (64-bit).

在 Windows 10(64 位)上使用 XAMPP 7.1.11 进行测试。



Schtasks.exe | Microsoft Docs

Schtasks.exe | 微软文档

Schtasks - Scheduled tasks - Windows CMD - SS64.com

Schtasks - 计划任务 - Windows CMD - SS64.com

PHP: CLI and CGI - Manual(php.exe vs php-win.exe)

PHP:CLI 和 CGI​​ - 手册(php.exe 与 php-win.exe)

回答by jabaldonedo

Cron is a Unix application for scheduled tasks, to get the same result under Windows you will need to use Task Manager.

Cron 是用于计划任务的 Unix 应用程序,要在 Windows 下获得相同的结果,您需要使用任务管理器。

First you create a simple task that start at 0:00, every day. Then, you go to Advanced... (or similar depending on the operating system you are on) and select the Repeat every 60 minutes.

首先,您创建一个简单的任务,每天从 0:00 开始。然后,您转到高级...(或类似,取决于您使用的操作系统)并选择每 60 分钟重复一次。