PHP and Crontab and Current Directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • roger.moon2@googlemail.com

    #1

    PHP and Crontab and Current Directory

    So I wrote a script and at the top it checks whether a file exists
    like below

    if (!file_exists(" settings.ini"))
    return false;

    and the crontab entry:

    00 08 * * 4 /usr/local/bin/php /home/user/scripts/test.php /dev/null
    2>&1

    however everytime it runs via cron, it can't find the file
    settings.ini because it keeps looking for it in $HOME not in $HOME/
    scripts/ where test.php resides

    Why? And how can I re-code it to look in the current directory not in
    $HOME? I tried ./settings.ini but without success

  • Rik

    #2
    Re: PHP and Crontab and Current Directory

    Euhm, check my reply to your previous post?

    --
    Rik Wasmus

    Estimated date being able to walk again: 01-05-2007.
    Less then a week, hurray!

    Comment

    • Michael Placentra II

      #3
      Re: PHP and Crontab and Current Directory

      roger.moon2@goo glemail.com wrote:
      So I wrote a script and at the top it checks whether a file exists
      like below
      >
      if (!file_exists(" settings.ini"))
      return false;
      >
      and the crontab entry:
      >
      00 08 * * 4 /usr/local/bin/php /home/user/scripts/test.php /dev/null
      2>&1
      >
      however everytime it runs via cron, it can't find the file
      settings.ini because it keeps looking for it in $HOME not in $HOME/
      scripts/ where test.php resides
      >
      Why? And how can I re-code it to look in the current directory not in
      $HOME? I tried ./settings.ini but without success
      >
      You can change the working directory with chdir() near the top of your script:

      #!/usr/local/bin/php -q
      <?php
      chdir( '/home/user/scripts' );

      //...
      ?>

      That should make file_exists() work as expected.

      If it doesn't do what you want it to do, you can try instead using:

      ini_set( 'include_path', '/home/user/scripts' );

      To temporarily set it as the (only) include path in the ini settings.

      -Mike PII

      Comment

      • Michael Placentra II

        #4
        Re: PHP and Crontab and Current Directory

        Sorry Rik, didn't notice your reply to the last one (I didn't even notice the last one).

        -Mike PII

        Comment

        • Rik

          #5
          Re: PHP and Crontab and Current Directory

          Michael Placentra II wrote:
          Sorry Rik, didn't notice your reply to the last one (I didn't even
          notice the last one).

          Hehehe, neither did he :-)

          --
          Rik Wasmus

          Estimated date being able to walk again: 01-05-2007.
          Less then a week, hurray!

          Comment

          • roger.moon2@googlemail.com

            #6
            Re: PHP and Crontab and Current Directory

            On Apr 25, 10:26 pm, Rik <luiheidsgoe... @hotmail.comwro te:
            Euhm, check my reply to your previous post?
            >
            Sorry about that, didn't show up and thought I had crossposted to too
            many groups and got halted by anti-spam scripts.

            Anyway your advice worked however the problem is I have to edit the
            script everytime I run the program via Cron

            Instead after some googling I found this method of instead:

            00 11 * * * cd /home/user1/files/; /usr/local/bin/php test.php

            Comment

            Working...