2 Replies Latest reply: Sep 27, 2011 12:23 PM by FlyingGuy RSS

    Executing another php file

    588264
      Hi

      I am new to php programming
      As part of one activity I new to schedule below three php files from cronjob at 10,11,12PM respectively

      0 10 * * * php -q /home/produsr/newtask22/gather.php
      0 11 * * * php -q /home/produsr/newtask22/upload.php
      0 12 * * * php -q /home/produsr/newtask22/report.php
      Now due to some issue by scheduling 3 cron jobs, I need to run above three php files from one php like

      #start newtask22
      execute php -q /home/produsr/newtask22/gather.php
      sleep 1hour #
      execute php -q /home/produsr/newtask22/upload.php
      sleep 1hour
      execute php -q /home/produsr/newtask22/report.php
      #end newtask22
      Please let me know how to do so in php

      I tried and wrote below script but not sure whether its right or not
      <?php
          exec("php -q /home/produsr/newtask22/gather.php");
          sleep(3600);
          exec("php -q /home/produsr/newtask22/upload.php");
          sleep(3600);
          exec("php -q /home/produsr/newtask22/report.php");
      ?> 
      Please correct my code :)
        • 1. Re: Executing another php file
          cj
          You could naively do:
          <?php
              require("/home/produsr/newtask22/gather.php");
              sleep(3600);
              require("/home/produsr/newtask22/upload.php");
              sleep(3600);
              require("/home/produsr/newtask22/report.php");
          ?> 
          However you might want to review & refactor the code since it will be "long running" and might hold onto resources unnecessarily.
          • 2. Re: Executing another php file
            FlyingGuy
            Running php from the cron scheduler works as you know but there are ways to do this better and still use cron.

            Wrap your jobs into a shell script. This way you can get the PID of the running job and then test to see if each job is finished prior to launching the next one.

            Just a thought