Use php to set cron jobs in Windows Use php to set cron jobs in Windows windows windows

Use php to set cron jobs in Windows


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 taskClick on start > All Programs > Accessories > System Tools > Scheduled Tasks

Or you can go directlyControl Panel > Scheduled Tasks

Right click in the folderNew > Schedule Task

Give appropriate name to the Task.In the RUN text field… Type the complete path of the CRON.BAT filein 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 OFFc:cd C:\Program Files\Internet ExplorerSTART 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 PHPUsing cron manager from within PHP

Managing Cron Jobs with PHPhttp://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.


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.


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.