6

I have written a PHP script which generates an SQL file containing all tables in my database. What I want to do is execute this script daily or every n days. I have read about cron jobs but I am using Windows. How can I automate the script execution on the server?

BSMP
  • 4,596
  • 8
  • 33
  • 44
chupinette
  • 456
  • 3
  • 9
  • 22

7 Answers7

4

You'll need to add a scheduled task to call the URL.

First of all, read up here: MS KB - this is for Windows XP.

Second, you'll need some way to call the URL - i'd recommend using something like wget - this way you can call the URL and save the output to a file, so you can see what the debug output is. You can get hold of wget on this page.

Final step is, as Gabriel says, write a batch file to tie all this up, then away you go.

e: wget is pretty simple to use, but if you have any issues, leave a comment and I'll help out.

ee: thinking about it, you don't even really need a batch file, and could just call wget directly..

danp
  • 14,876
  • 6
  • 42
  • 48
  • @danp : Hello!Thanks a lot for the links. I have a question, concerning the first link, if my application is on the running on a server how do i Add a Schedule Task? – chupinette May 04 '10 at 09:24
  • 1
    There are two things here - the first is the server, and the second is where you call the script on the server *from*. Where you call the script from can be anywhere, it can be on the same machine, or on a different remote machine. Imagine you have the script on www.mybox.com/dailyscript.php - on your office or home machine you add a scheduled task which calls that script using wget (or whatever) and basically, it should "Just Work". – danp May 04 '10 at 09:40
  • when adding a schedule task, i need to specify the action that will occur and its details. I have selected: Start a program and http://localhost/final/myscript.php. Is that correct? – chupinette May 05 '10 at 11:28
  • it may work, but if you change it to "wget localhost/final/myscript.php" then for sure it'll be fine. Put a target for the file it gets and that should be done. – danp May 05 '10 at 13:26
  • i have changed it to wget but it does not seem to work. Can you help me please? – chupinette May 05 '10 at 14:40
  • C:\Program files\GnuWin32\bin>wget I get : SYSTEM_WGETRC = c:progra.... syswgetrc = C:\Program Files.. wget: missing URL – chupinette May 05 '10 at 15:46
  • 1
    and if you do "wget localhost/final/myscript.php"? – danp May 05 '10 at 16:09
  • Yes it works! myscript.php is supposed to send a mail. The mail successfully sent. So now that it works from command line, can you guide me how to solve the problem? – chupinette May 05 '10 at 16:20
  • ive found my mistake! Thanks a lot danp! i should have written "C:\Program Files\GnuWin32\bin\wget.exe" http://localhost/final/myscript.php – chupinette May 05 '10 at 16:24
  • no probs :) I thought that what was going on was that the path to wget was not in the "PATH ... " environment variable, so it couldn't be found... If you want to add it in, just add the directory where wget is to your environment, but be careful, as it's pretty easy to break stuff. More info here: http://support.microsoft.com/kb/310519 – danp May 05 '10 at 18:04
  • @dmp i have doubt in cron job may i ask? – user200 Nov 29 '17 at 09:47
2

add a scheduled task to request the url. either using a batch file or a script file (WSH).

http://blog.netnerds.net/2007/01/vbscript-download-and-save-a-binary-file/

this script will allow you to download binary data from a web source. Modify it to work for you particular case. This vbs file can either be run directly or executed from within a script. Alternately you do not have to save the file using the script, you can just output the contents (WScript.Echo objXMLHTTP.ResponseBody) and utilize the CMD out to file argument:

cscript download.vbs > logfile.log

save that bad boy in a .bat file somewhere useful and call it in the scheduler: http://lifehacker.com/153089/hack-attack-using-windows-scheduled-tasks

Gabriel
  • 18,322
  • 2
  • 37
  • 44
1

You can use ATrigger scheduling service. A PHP library is also available to create scheduled tasks without overhead. Reporting, Analytics, Error Handling and more benefits.

Disclaimer: I was among the ATrigger team. It's a freeware and I have not any commercial purpose.

1

Cron is not always available on many hosting accounts.

But try this: http://www.phpjobscheduler.co.uk/ its free, has a useful interface so you can see all the scheduled tasks and will run on any host that provides php and mysql.

0

Yes, You can schedule and execute your php script on windows to run automatically. In linux like os u will have cron but on windows u can schedule task using task scheduler.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
mukund
  • 2,253
  • 1
  • 18
  • 31
0

Windows doesn't have cron, but it does come with the 'at' command. It's not as flexible as cron, but it will allow you to schedule arbitrary tasks for execution from the command line.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

If your code is in remote hosted server then create a cron-job for the same. Else if in local then use a scheduled task in windows.Its easy to implement.I am having servers with so many scheduled tasks running.

arun s r
  • 57
  • 6