Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

Use php to set cron jobs in Windows

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:cmsxamppphpphp.exe c:cmsxampphtdocsexecutecron.php
      

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

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

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:xamppphpphp.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:xamppphpphp.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:Windowswget.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:Windowswget.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:xamppphpphp.exe D:xampphtdocsApplicationcron.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:xampphtdocsApplicationCRON.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 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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...