Use php to set cron jobs in Windows

85,739

Solution 1

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.

Solution 2

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.

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:

schtasks /create /tn UNO /tr YOURAPP.EXE /sc HOURLY /mo 2

See the official MS support page for more info on this nifty application.

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.

Hope that helps, happy coding friend.

Solution 3

Another great tool is available for free on Windows, nncron. It uses the exact same syntax than unix' cron:

http://www.nncron.ru/

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.

Share:
85,739
Leo Chan
Author by

Leo Chan

Updated on February 28, 2020

Comments

  • Leo Chan
    Leo Chan about 4 years

    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

  • Leo Chan
    Leo Chan about 12 years
    yes, that is what i want to do , i have a sendmail.php , and user input the date, the server will run the sendmail.php on that date . What I would like to know is the workflow after user submit the date, host side have to add user job request manually in os?
  • stefgosselin
    stefgosselin about 12 years
    Your cron script would check once a day and select all messages where db_message date >= today's date, then loop through the resultset and send the messages. Another small detail, I recommend you have a boolean field in your message table to act as a 'flag', indicating if message was sent. This way you could select all non sent messages where messageDate >= currentDate, you would be certain to never mailbomb anybody, when message is sent you set flag to 1. If you have any problems, feel free to message me.
  • Pacerier
    Pacerier over 9 years
    @stefgosselin, What does NSIS do?
  • stefgosselin
    stefgosselin over 9 years
    @Pacerier - it is in app to write installers, it packages an executable to install an application on windows. THe application (NSI) has a feature that automates the creation of a scheduled task, Of course the task scheduer can be configured manually to run 'x' application every 'x' time, but with NSI you can replicate easily the same rule and have the benefit of being able to just run the generated .exe in any machine and have the scheduled task created automatically.
  • Pacerier
    Pacerier over 9 years
    @stefgosselin, So internally NSIS uses schtasks too right?
  • stefgosselin
    stefgosselin over 9 years
    @Pacerier - Yes. It automates the creation of a scheduled task. As a bonus, the uninstaller removes the scheduled tast. FOr example, I have previously used NSI to (easily) create a package that installed cleansweep, disk defraggler and created scheduled tasks for both.
  • Moxet Khan
    Moxet Khan about 8 years
    The task schedule on localhost works like charm! thats what i need for a web-app running on localhost. Thank you..
  • Gem
    Gem over 6 years
    @MoxetKhan could you pls share how you set in localhost,
  • Adrian P.
    Adrian P. over 6 years
    @Rathinam Chose one solution above and follow the steps. Solution-4 is the best. technet.microsoft.com/en-us/library/cc766428(v=ws.11).aspx
  • Gem
    Gem over 6 years
    @AdrianP. IS possible to use wamp
  • Moxet Khan
    Moxet Khan over 6 years
    Yes it is possible with WAMP and XAMPP. Try solution 4 if you face any trouble pleas mention here..
  • Gem
    Gem over 6 years
    How can i write cron.php file, my need , if order placed the email send to customer and copy to admin
  • Taki Elias
    Taki Elias about 3 years
    How to use this? I could not get any documentation.