Calling php script with cron - need shebang?

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

    Calling php script with cron - need shebang?

    In regard to running php scripts with cron -

    Here is a sample script:

    <?php
    //debug.php
    echo "<br> This is a test";
    ?>

    I can call debug.php from a web page on my site like this:

    include 'debug.php';

    But what if I want to call that same script using cron?

    Do I need to modify the script like this:

    #!/usr/bin/php -q
    <?php
    //debug.php
    echo "<br> This is a test";
    ?>

    Do I need to include the shebang (#!/usr/bin/php -q) for a php script to be
    called from cron?


  • oeb -=- bleh bleh bleh

    #2
    Re: Calling php script with cron - need shebang?


    "deko" <nospam@hotmail .com> wrote in message
    news:0IbJc.1033 1$B71.7440@news svr25.news.prod igy.com...[color=blue]
    > In regard to running php scripts with cron -
    >
    > Here is a sample script:
    >
    > <?php
    > //debug.php
    > echo "<br> This is a test";
    > ?>
    >
    > I can call debug.php from a web page on my site like this:
    >
    > include 'debug.php';
    >
    > But what if I want to call that same script using cron?
    >
    > Do I need to modify the script like this:
    >
    > #!/usr/bin/php -q
    > <?php
    > //debug.php
    > echo "<br> This is a test";
    > ?>
    >
    > Do I need to include the shebang (#!/usr/bin/php -q) for a php script to[/color]
    be[color=blue]
    > called from cron?[/color]

    If you are calling the php script from the command line without shebang you
    will need to execute it by typing
    $ php script.php

    With a shebang it can be called as follows
    $ script.php

    makesure you chmod it +x though, otherwise it won't run at all.

    For a tutorial on PHP on the Command line



    Comment

    • deko

      #3
      Re: Calling php script with cron - need shebang?

      > If you are calling the php script from the command line without shebang
      you[color=blue]
      > will need to execute it by typing
      > $ php script.php
      >
      > With a shebang it can be called as follows
      > $ script.php[/color]

      So it is only when I call the script from the command line that I need the
      shebang? Then no modification is needed to run my script using cron...
      that's what I thought, but wasn't sure.... thanks...


      Comment

      • Herbie Cumberland

        #4
        Re: Calling php script with cron - need shebang?

        On Wed, 14 Jul 2004 15:15:04 GMT, "deko" <nospam@hotmail .com> wrote:
        [color=blue][color=green]
        >> If you are calling the php script from the command line without shebang[/color]
        >you[color=green]
        >> will need to execute it by typing
        >> $ php script.php
        >>
        >> With a shebang it can be called as follows
        >> $ script.php[/color]
        >
        >So it is only when I call the script from the command line that I need the
        >shebang? Then no modification is needed to run my script using cron...
        >that's what I thought, but wasn't sure.... thanks...[/color]

        no.

        running a script from a cron job is the same as running it from the
        command line (more or less, see note [1] below).

        from the command line, you can invoke a php script in two ways:

        $ php [-q] /path/to/script.php

        or

        $ /path/to/script.php

        in the second method, the script _must_ have a shebang specifying the
        location of your php binary, eg. #!/usr/local/bin/php

        entries in your crontab are the same, eg:

        45 3 * * * /usr/local/bin/php -q /path/to/script.php

        or

        45 3 * * * /path/to/script.php

        in the second method the script must also have a shebang.


        [1] note that your cron job will almost certainly not know about any
        PATH information you may have set up in your shell, so you must always
        give the full path to the php binary if you are invoking your script
        using the first method in the case above.





        Comment

        • Savut

          #5
          Re: Calling php script with cron - need shebang?

          Note that Apache ENV variables doesn't exists when you run it in shell.

          Savut


          "deko" <nospam@hotmail .com> wrote in message
          news:Y5cJc.1034 2$ym1.9057@news svr25.news.prod igy.com...[color=blue][color=green]
          >> If you are calling the php script from the command line without shebang[/color]
          > you[color=green]
          >> will need to execute it by typing
          >> $ php script.php
          >>
          >> With a shebang it can be called as follows
          >> $ script.php[/color]
          >
          > So it is only when I call the script from the command line that I need the
          > shebang? Then no modification is needed to run my script using cron...
          > that's what I thought, but wasn't sure.... thanks...
          >
          >[/color]

          Comment

          • R. Rajesh Jeba Anbiah

            #6
            Re: Calling php script with cron - need shebang?

            "deko" <nospam@hotmail .com> wrote in message news:<0IbJc.103 31$B71.7440@new ssvr25.news.pro digy.com>...
            <snip>[color=blue]
            > Do I need to include the shebang (#!/usr/bin/php -q) for a php script to be
            > called from cron?[/color]

            FWIW, for most of the servers, this method won't work as they use
            mod_php. So, in that case you need to use wget.

            --
            | Just another PHP saint |
            Email: rrjanbiah-at-Y!com

            Comment

            Working...