1

I have a situation where i need to process a large volume of database data once a day for my php application. My application is deployed on azure, it is a webapp and is built on codeigniter v3.x. I have been able to run a schedule on my local window machine using a .bat file and a .php file.

here is the content of my batch file

start D:\xampp\php\php.exe -f D:\xampp\htdocs\phpcode\cron.php

this I can write because i know the exact path of php. Right now there is a simple php file cron.php which does the job. But what i plan is to write a controller class and call its function from the scheduler.

start <the path of php executable on azure> <call to something like 'http://www.example.com/controllername/functionname>

Please advise. Any other solution will also be helpful

3 Answers3

2

Few additions to @mandrax answer.

  • You do not need the batch file, just use the php file as your WebJob, Azure will run it.

  • You can use the WebJob scheduler described in this answer, basically you just add a file called settings.job to your WebJob and describe the schedule there as a cron expression.

    For example {"schedule": "0 0 0 * * *"} for once a day at midnight.

Community
  • 1
  • 1
Amit Apple
  • 9,034
  • 41
  • 50
2

I wanted to have a webjob to run using CodeIgniter's MVC structure, but nobody seemed to answer that question anywhere I could find. After hours of searching and testing, here is the answer.

You need a batch file to manually call php.exe with CI's index as the page and the controller and method as parameters. In this example, my controller is called "cron" and my method is "clean_uploads". If you wanted to access it manually in your browser, you'd go to http://yoursite.com/cron/clean_uploads

REM Set PHP location
SET php="D:\Program Files (x86)\PHP\v5.6\php.exe"

REM Set Index Location
SET index="D:\home\site\wwwroot\index.php"

REM Set Controller
SET controller="cron"

REM Set Method
SET method="clean_uploads"

REM Run Command
%php% %index% %controller% %method%

To find the location of php.exe and index.php, you'll need to run the following inside a CI Controller:

$php_location   = exec("where php.exe");
$index_location = FCPATH . "index.php";

echo 'SET php="' . $php_location . '"' . "\n";
echo 'SET index="' . $index_location . '"' . "\n";

If you want to disable access to your controller via the browser, add the following to your constructor:

    if (php_sapi_name() != "cli") {
        show_404();
    }

Hope that helps someone else!

Yahtzie
  • 21
  • 1
1

As you want to run your scripts once a day, there is quite an easy solution there, please tell me if it does not meet your needs, There are others :).

  1. create 2 zip files containing your php and bat file (1 file per zip)
  2. go to the old Azure portal (https://manage.windowsazure.com)
  3. go to your website, select the webjob tab.
  4. Press on add a job
  5. upload one of your Zip file and in the "How to run" selection box, select "run on schedule", press next
  6. On the next screen select "recurring job", and recur every one day.
  7. repeat from point 4 with your other script

The job will now run each day on the time you set up! Tell me if the solution fix your problems!

Cheers,

Mike

Mandur
  • 153
  • 10
  • 1
    I was going to add you screenshots but I realized that there is already quite well explained there : https://azure.microsoft.com/en-us/documentation/articles/web-sites-create-web-jobs/ if you scroll to the "Create a scheduled WebJob" section – Mandur Aug 18 '15 at 08:46
  • Actually i have gone through the link you provided. And i know how to do it using the portal, But my problem is about the content of the batch file. Where the first parameter after the Start command is the path of php.exe, I don't know how i can get that on azure. A running example of a batch file will be most helpful – vishwajeet kumar Aug 18 '15 at 09:47
  • Hello! I clearly did not figure out the problem correctly. If you make an azure webjob, you actually don't need to call the php command, it will be called directly for you, you basically just have to upload the file D:\xampp\htdocs\phpcode\cron.php (from your example here above) on Azure as a webjob. If you want an example, just take this simple script save it to a php file, zip it, upload it on Azure as in my answer. then go to the kudu url of your website https://.scm.azurewebsites.net/azurejobs/#/jobs ,click on your job and see it running! – Mandur Aug 18 '15 at 11:00
  • Azure will call automatically the php.exe, if you want to run manually or to see the arborescence of the files, go to the kudu portal. You can find the install directory on the environment tab, and you can get the console with the "Debug console" tab – Mandur Aug 18 '15 at 11:07