By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,068 Members | 1,734 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,068 IT Pros & Developers. It's quick & easy.

Calling php script with cron - need shebang?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
"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.