browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need PHP help?

Get answers from our community of PHP experts on BYTES! It's free.

Calling php script with cron - need shebang?

deko
Guest
 
Posts: n/a
#1: Jul 17 '05
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
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Calling php script with cron - need shebang?



"deko" <nospam@hotmail.com> wrote in message
news:0IbJc.10331$B71.7440@newssvr25.news.prodigy.c om...[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
http://www.sitepoint.com/article/php-command-line-1


deko
Guest
 
Posts: n/a
#3: Jul 17 '05

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...


Herbie Cumberland
Guest
 
Posts: n/a
#4: Jul 17 '05

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.





Savut
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Calling php script with cron - need shebang?


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

Savut
http://www.savut.com

"deko" <nospam@hotmail.com> wrote in message
news:Y5cJc.10342$ym1.9057@newssvr25.news.prodigy.c om...[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]

R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Calling php script with cron - need shebang?


"deko" <nospam@hotmail.com> wrote in message news:<0IbJc.10331$B71.7440@newssvr25.news.prodigy. 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
Closed Thread