Cron Job For Running A Controller Action

I want to run a controller action in every 2 hours in my application for checking any updates in xml url data. How perform this in cron jobs in Windows and Linux?

If hosting not allowed the cron jobs, when it can perform in yii?

may be you can use the js timer with ajax to trigger the action;

in layout file or somewhere to register js code to do that . every request come in first to check a unique file (if not exist create it ) the last modified time if meet your requirement then register js timer code (registerScript).

You can also define a publicly accessible URL with a GET parameter and access that page from another server on a scheduled basis.

Lets say you do something like this:

Make sure you check the GET key is correct. You could also limit it to an IP address. That way you can fire scheduled actions from another server you do have cron job access on and it is platform independent.

This method is useful without open the application?

if yes can you explain the steps for that?

layout or some where to register some code like this :

 var cronTimer ;


              cronTimer =   setInterval(cronTask,3000)


        function cronTask(){

             // get is ok !




                    //  here check if the cron executed in server end , if true stop the cronTimer using clearInterval(cronTimer)





in your server end you should track the last executed time by some storage (db or file )?

         // in some controller 

            function actionCron(){


                 check if is ajax and security key is allowed 

                 check if the cron task is executed (if you using file to do the track .check file ctime/mtime : filemtime().. if file not exist just create it ! )

                 if  ((now - (lastExecutingTime)) < (twoHours - delta)){

                       response some thing    

                  } else{

                     it is the time we should do the cron job !

                     then we update the file time to indicate the lastExecutingTime

                     response the cron is executed !




above is just my thoughts , i never do it myself ;

for whether should we start the timer , in server side we can check the lastExecutingTime if it is close to some value then we register the first section js codes to layout or some where

this way to do the cron job is not so accurate as *nix or windows (crontab and taskschedule)

for security key(just one possible way) : (Yii::app()->request->csrfTokenName.’=’.Yii::app()->request->getCsrfToken())

anyway this is what i can give you :D