472,127 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

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?
Jul 17 '05 #1
5 9071

"deko" <no****@hotmail.com> wrote in message
news:0I******************@newssvr25.news.prodigy.c om...
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?


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
Jul 17 '05 #2
> 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


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...
Jul 17 '05 #3
On Wed, 14 Jul 2004 15:15:04 GMT, "deko" <no****@hotmail.com> wrote:
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


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


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.

Jul 17 '05 #4
Note that Apache ENV variables doesn't exists when you run it in shell.

Savut
http://www.savut.com

"deko" <no****@hotmail.com> wrote in message
news:Y5******************@newssvr25.news.prodigy.c om...
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


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


Jul 17 '05 #5
"deko" <no****@hotmail.com> wrote in message news:<0I******************@newssvr25.news.prodigy. com>...
<snip>
Do I need to include the shebang (#!/usr/bin/php -q) for a php script to be
called from cron?


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
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Michael Appelmans | last post: by
29 posts views Thread by deko | last post: by
7 posts views Thread by paranoid-android | last post: by
2 posts views Thread by volantecho | last post: by
6 posts views Thread by metaperl | last post: by
4 posts views Thread by J. Frank Parnell | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.