PHP, cron and stdout redirection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil

    PHP, cron and stdout redirection

    I have a php script that queries some Oracle DB and outputs a single
    line of plain text with <brat the end for each query. This is
    Apache2, php4.4.8 and Oracle Instant Client 10.1.0.5 all on CentOS 4.4
    32bit. Php does run as an Apache SO.

    It works perfectly if I just hit the page in a web-browser (output to
    browser).

    It works *perfectly* from the command line (dumps the text to the file
    per the redirect)
    /usr/bin/php /var/www/html/dashboard/dash1.php /var/www/html/
    dashboard/dash.inc

    When run as cron, it updates the timestamp of the target file, but
    does not write the text, nor any error.
    05,15,25,35,45, 55 * * * * /usr/bin/php -q /var/www/html/dashboard/
    dash1.php /var/www/html/dashboard/dash.inc

    There is nothing in php error log, syslog or any other location I can
    find. The entry in cron log suggests that it works perfectly. It does
    not email any errors to me.

    I'm very puzzled.... anyone have any idea for trouble shooting ????

    philc
    (this is not hosted, I control/operate the linux server)
  • The Natural Philosopher

    #2
    Re: PHP, cron and stdout redirection

    Phil wrote:
    I have a php script that queries some Oracle DB and outputs a single
    line of plain text with <brat the end for each query. This is
    Apache2, php4.4.8 and Oracle Instant Client 10.1.0.5 all on CentOS 4.4
    32bit. Php does run as an Apache SO.
    >
    It works perfectly if I just hit the page in a web-browser (output to
    browser).
    >
    It works *perfectly* from the command line (dumps the text to the file
    per the redirect)
    /usr/bin/php /var/www/html/dashboard/dash1.php /var/www/html/
    dashboard/dash.inc
    >
    When run as cron,
    Whose cron? with what permissions?

    >it updates the timestamp of the target file, but
    does not write the text, nor any error.
    05,15,25,35,45, 55 * * * * /usr/bin/php -q /var/www/html/dashboard/
    dash1.php /var/www/html/dashboard/dash.inc
    >
    There is nothing in php error log, syslog or any other location I can
    find. The entry in cron log suggests that it works perfectly. It does
    not email any errors to me.
    >
    I'm very puzzled.... anyone have any idea for trouble shooting ????
    >
    Work out what effective user cron is running the script at, 'su -' to
    that user, and run the exact command line from there.


    Or try setting the cron entry to write to a brand new file in /tmp,
    which is world writable, and see what permissions it turns up with.

    and indeed whether it works...at all.
    philc
    (this is not hosted, I control/operate the linux server)

    Comment

    • AnrDaemon

      #3
      Re: PHP, cron and stdout redirection

      Greetings, Phil.
      In reply to Your message dated Wednesday, February 20, 2008, 04:19:52,
      I have a php script that queries some Oracle DB and outputs a single
      line of plain text with <brat the end for each query. This is
      Apache2, php4.4.8 and Oracle Instant Client 10.1.0.5 all on CentOS 4.4
      32bit. Php does run as an Apache SO.
      It works perfectly if I just hit the page in a web-browser (output to
      browser).
      It works *perfectly* from the command line (dumps the text to the file
      per the redirect)
      /usr/bin/php /var/www/html/dashboard/dash1.php /var/www/html/
      dashboard/dash.inc
      When run as cron, it updates the timestamp of the target file, but
      does not write the text, nor any error.
      05,15,25,35,45, 55 * * * * /usr/bin/php -q /var/www/html/dashboard/
      dash1.php >/var/www/html/dashboard/dash.inc
      There is nothing in php error log, syslog or any other location I can
      find. The entry in cron log suggests that it works perfectly. It does
      not email any errors to me.
      I'm very puzzled.... anyone have any idea for trouble shooting ????
      I think Your problem is redirection in cron job. You can't be sure what is
      redirected to the file.
      Redone it as follows:


      <?php

      ob_start();
      include_once('/var/www/html/dashboard/dash1.php');
      $rc = ob_get_contents ();
      ob_end_clean();

      $f = fopen('/var/www/html/dashboard/dash.inc', 'ab');
      fwrite($f, $rc);
      fclose($f);

      ?>


      --
      Sincerely Yours, AnrDaemon <anrdaemon@free mail.ru>

      Comment

      • Phil

        #4
        Re: PHP, cron and stdout redirection

        On Feb 20, 8:42 am, AnrDaemon <anrdae...@free mail.ruwrote:
        Greetings, Phil.
        In reply to Your message dated Wednesday, February 20, 2008, 04:19:52,
        >
        I have a php script that queries some Oracle DB and outputs a single
        line of plain text with <brat the end for each query. This is
        Apache2, php4.4.8 and Oracle Instant Client 10.1.0.5 all on CentOS 4.4
        32bit. Php does run as an Apache SO.
        It works perfectly if I just hit the page in a web-browser (output to
        browser).
        It works *perfectly* from the command line (dumps the text to the file
        per the redirect)
           /usr/bin/php /var/www/html/dashboard/dash1.php  /var/www/html/
        dashboard/dash.inc
        When run as cron, it updates the timestamp of the target file, but
        does not write the text, nor any error.
            05,15,25,35,45, 55 * * * * /usr/bin/php -q  /var/www/html/dashboard/
        >
        dash1.php  >/var/www/html/dashboard/dash.inc
        >
        There is nothing in php error log, syslog or any other location I can
        find. The entry in cron log suggests that it works perfectly. It does
        not email any errors to me.
        I'm very puzzled.... anyone have any idea for trouble shooting ????
        >
        I think Your problem is redirection in cron job. You can't be sure what is
        redirected to the file.
        Redone it as follows:
        >
        <?php
        >
        ob_start();
        include_once('/var/www/html/dashboard/dash1.php');
        $rc = ob_get_contents ();
        ob_end_clean();
        >
        $f = fopen('/var/www/html/dashboard/dash.inc', 'ab');
        fwrite($f, $rc);
        fclose($f);
        >
        ?>
        >
        --
        Sincerely Yours, AnrDaemon <anrdae...@free mail.ru>
        I might try that ... the work around that I already implemented was to
        use a wget <url-O <output filein cron. Seemed kinda hack-ish, but
        it did work :-)

        I suspect the root cause is related to the environment, since that is
        also a somewhat common problem that unix people have with other cron
        scripts (shell, perl, python, etc), but not sure how to overcome it
        for php in cron. Anyway, it was a rather unusual situation were I
        wanted to use the same code for a live-page with DB lookups, and for
        an cron-scripted output to a page for the same DB lookups.

        Thanks All. Unless anyone a further brain-storm about the env for cron
        php, I think this is a closed issue.

        Comment

        • The Natural Philosopher

          #5
          Re: PHP, cron and stdout redirection

          Phil wrote:
          On Feb 20, 8:42 am, AnrDaemon <anrdae...@free mail.ruwrote:
          >Greetings, Phil.
          >In reply to Your message dated Wednesday, February 20, 2008, 04:19:52,
          >>
          >>I have a php script that queries some Oracle DB and outputs a single
          >>line of plain text with <brat the end for each query. This is
          >>Apache2, php4.4.8 and Oracle Instant Client 10.1.0.5 all on CentOS 4.4
          >>32bit. Php does run as an Apache SO.
          >>It works perfectly if I just hit the page in a web-browser (output to
          >>browser).
          >>It works *perfectly* from the command line (dumps the text to the file
          >>per the redirect)
          >> /usr/bin/php /var/www/html/dashboard/dash1.php /var/www/html/
          >>dashboard/dash.inc
          >>When run as cron, it updates the timestamp of the target file, but
          >>does not write the text, nor any error.
          >> 05,15,25,35,45, 55 * * * * /usr/bin/php -q /var/www/html/dashboard/
          >dash1.php >/var/www/html/dashboard/dash.inc
          >>
          >>There is nothing in php error log, syslog or any other location I can
          >>find. The entry in cron log suggests that it works perfectly. It does
          >>not email any errors to me.
          >>I'm very puzzled.... anyone have any idea for trouble shooting ????
          >I think Your problem is redirection in cron job. You can't be sure what is
          >redirected to the file.
          >Redone it as follows:
          >>
          ><?php
          >>
          >ob_start();
          >include_once ('/var/www/html/dashboard/dash1.php');
          >$rc = ob_get_contents ();
          >ob_end_clean() ;
          >>
          >$f = fopen('/var/www/html/dashboard/dash.inc', 'ab');
          >fwrite($f, $rc);
          >fclose($f);
          >>
          >?>
          >>
          >--
          >Sincerely Yours, AnrDaemon <anrdae...@free mail.ru>
          >
          I might try that ... the work around that I already implemented was to
          use a wget <url-O <output filein cron. Seemed kinda hack-ish, but
          it did work :-)
          >
          I suspect the root cause is related to the environment, since that is
          also a somewhat common problem that unix people have with other cron
          scripts (shell, perl, python, etc), but not sure how to overcome it
          for php in cron. Anyway, it was a rather unusual situation were I
          wanted to use the same code for a live-page with DB lookups, and for
          an cron-scripted output to a page for the same DB lookups.
          >
          Thanks All. Unless anyone a further brain-storm about the env for cron
          php, I think this is a closed issue.

          I have a sneaking suspicion it may be due to the way PHP does its
          buffering. The fact that a file is CREATED means there are no perm problems.


          Not sure what the actual PHP commands are, but the equivalent of an
          fflush();fclose ();sync(); on stdout is something I would try..

          Comment

          Working...