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

a PHP script works fine when called from the browser but not as a shell script

P: n/a
I've got a script called makeRss.php. It works fine if I try to open it
with my browser. It makes an RSS feed for every page on my site. You
can see it working here:

http://www.autostratus.com/index.php?whatPage=makeRss

It does just what I want it to do.

However, I need for this script to get called every 15 minutes. So I
set it up as a cron job. And cron doesn't like calling this thing. Cron
is nice enough to send me an email with an error message. Every 15
minutes I get this:

/home/httpd/vhosts/autostratus.com/httpdocs/makeRss.php: line 1: ?php:
No such file
or directory
/home/httpd/vhosts/autostratus.com/httpdocs/makeRss.php: line 2: syntax
error near
unexpected token `"tagIndexLibrary.php"'
/home/httpd/vhosts/autostratus.com/httpdocs/makeRss.php: line 2:
`include_once("tagIndexLibrary.php");
Why would cron have a problem if the script works fine from the web
browser?

Dec 21 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On 21 Dec 2005 09:06:53 -0800, "lawrence k" <lk******@geocities.com> wrote:
I've got a script called makeRss.php. It works fine if I try to open it
with my browser. It makes an RSS feed for every page on my site. You
can see it working here:

http://www.autostratus.com/index.php?whatPage=makeRss

It does just what I want it to do.

However, I need for this script to get called every 15 minutes. So I
set it up as a cron job. And cron doesn't like calling this thing. Cron
is nice enough to send me an email with an error message. Every 15
minutes I get this:

/home/httpd/vhosts/autostratus.com/httpdocs/makeRss.php: line 1: ?php:
No such fil


You seem to be trying to run the .php file directly. This won't work; you
either need a #! line at the beginning, or you need to run it via PHP.

So either:

#!/path/to/php

as the first line, which will mess things up when it's run via the web, or
change your cron entry to:

*/15 * * * * /path/to/php /path/to/makeRss.php

... as I'm assuming it's currently something like:

*/15 * * * * /path/to/makeRss.php

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 21 '05 #2

P: n/a
In our last episode,
<11*********************@g49g2000cwa.googlegroups. com>,
the lovely and talented lawrence k
broadcast on comp.lang.php:
/home/httpd/vhosts/autostratus.com/httpdocs/makeRss.php: line 1: ?php:
No such file
or directory
/home/httpd/vhosts/autostratus.com/httpdocs/makeRss.php: line 2: syntax
error near
unexpected token `"tagIndexLibrary.php"'
/home/httpd/vhosts/autostratus.com/httpdocs/makeRss.php: line 2:
`include_once("tagIndexLibrary.php");
Why would cron have a problem if the script works fine from the web
browser?


The executable is php, not your php script. The server knows
this. The operating system does not.

The first two characters of your file need to be #! followed
immediately by the path to php (not makeRss.php) - and when I
say first two characters, I mean the first two characters with
no preceding empty lines or spaces. Or you can call php from
chron with your script as an argument.

--
Lars Eighner us****@larseighner.com http://www.larseighner.com/
My mail reader can beat up your mail reader.
Dec 21 '05 #3

P: n/a

Andy Hassall wrote:
On 21 Dec 2005 09:06:53 -0800, "lawrence k" <lk******@geocities.com> wrote:

However, I need for this script to get called every 15 minutes. So I
set it up as a cron job. And cron doesn't like calling this thing. Cron
is nice enough to send me an email with an error message. Every 15
minutes I get this:

/home/httpd/vhosts/autostratus.com/httpdocs/makeRss.php: line 1: ?php:
No such fil


You seem to be trying to run the .php file directly. This won't work; you
either need a #! line at the beginning, or you need to run it via PHP.

So either:

#!/path/to/php

as the first line, which will mess things up when it's run via the web, or


I ran phpinfo() to find out the path, and this is the info I got:

PATH /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin

_SERVER["PATH"] /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin

_SERVER["PATH_TRANSLATED"] /home/httpd/vhosts/autostratus.com/httpdocs/info.php

_ENV["PATH"] /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin
I'm sorry I know so little about shell scripts, but I can't figure from
this what the address in the first line should be. Is it like this:

#!/sbin/php

Dec 21 '05 #4

P: n/a
On 21 Dec 2005 15:32:30 -0800, "lawrence k" <lk******@geocities.com> wrote:
Andy Hassall wrote:
On 21 Dec 2005 09:06:53 -0800, "lawrence k" <lk******@geocities.com> wrote:
>However, I need for this script to get called every 15 minutes. So I
>set it up as a cron job. And cron doesn't like calling this thing. Cron
>is nice enough to send me an email with an error message. Every 15
>minutes I get this:
>
>/home/httpd/vhosts/autostratus.com/httpdocs/makeRss.php: line 1: ?php:
>No such fil


You seem to be trying to run the .php file directly. This won't work; you
either need a #! line at the beginning, or you need to run it via PHP.

So either:

#!/path/to/php

as the first line, which will mess things up when it's run via the web, or


I ran phpinfo() to find out the path, and this is the info I got:

PATH /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin

_SERVER["PATH"] /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin

_SERVER["PATH_TRANSLATED"] /home/httpd/vhosts/autostratus.com/httpdocs/info.php

_ENV["PATH"] /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin

I'm sorry I know so little about shell scripts, but I can't figure from
this what the address in the first line should be. Is it like this:

#!/sbin/php


Type the following at a shell prompt:

which php

This will tell you the path.

Failing that, use phpinfo(), and look for "--prefix=" in the configure line.
If it's "--configure=/usr", then the CLI version of PHP is at /usr/bin/php.

And you probably don't want to be adding the #! line due to the warnings I
wrote immediately after it. You probably want the second option, changing your
cron entry to call php with the script as an argument.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 22 '05 #5

P: n/a
On Wed, 21 Dec 2005 09:06:53 -0800, lawrence k wrote:
Why would cron have a problem if the script works fine from the web
browser?


Because you didn't specify the interpreter at the beginning of the script.
#!/usr/bin/php would be an excellent choice first line of the script,
if your php executable resides in /usr/bin.

--
http://www.mgogala.com

Dec 22 '05 #6

P: n/a
On Wed, 21 Dec 2005 17:42:34 +0000, Andy Hassall wrote:
*/15 * * * * /path/to/php /path/to/makeRss.php


This is disgusting! Andy, I'm disappointed.

--
http://www.mgogala.com

Dec 22 '05 #7

P: n/a
On Thu, 22 Dec 2005 00:51:16 GMT, Mladen Gogala <go****@sbcglobal.net> wrote:
On Wed, 21 Dec 2005 17:42:34 +0000, Andy Hassall wrote:
*/15 * * * * /path/to/php /path/to/makeRss.php


This is disgusting! Andy, I'm disappointed.


In what way?

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 22 '05 #8

P: n/a
In our last episode,
<11**********************@g49g2000cwa.googlegroups .com>, the
lovely and talented lawrence k broadcast on comp.lang.php:

Andy Hassall wrote:
On 21 Dec 2005 09:06:53 -0800, "lawrence k" <lk******@geocities.com> wrote:
>
>However, I need for this script to get called every 15 minutes. So I
>set it up as a cron job. And cron doesn't like calling this thing. Cron
>is nice enough to send me an email with an error message. Every 15
>minutes I get this:
>
>/home/httpd/vhosts/autostratus.com/httpdocs/makeRss.php: line 1: ?php:
>No such fil
You seem to be trying to run the .php file directly. This won't work; you
either need a #! line at the beginning, or you need to run it via PHP.

So either:

#!/path/to/php

as the first line, which will mess things up when it's run via the web, or

I ran phpinfo() to find out the path, and this is the info I got: PATH /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin _SERVER["PATH"] /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin _SERVER["PATH_TRANSLATED"] /home/httpd/vhosts/autostratus.com/httpdocs/info.php _ENV["PATH"] /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin
I'm sorry I know so little about shell scripts, but I can't figure from
this what the address in the first line should be. Is it like this: #!/sbin/php


run
which php

That returns (in FreeBSD, will almost certainly vary in other
unices):

/usr/local/bin/php

That's what goes in the she-bang line:
#!/usr/local/bin/php

("She-bang" is an informal expression for #!).

This works for many kinds of scripts etc. in other languages,
such as perl, various shells, etc.

However, others have pointed out, php is not entirely unbroken
in this regard, and it would be more convenient for you if you
only had one version of each script, so the better option for
you is to run php from chron with your script as an argument.

--
Lars Eighner us****@larseighner.com http://www.larseighner.com/
Good Morning, Carnivore! - Anthrax botulin nuclear flight mail Akbar reservoir
Ramadan letter activate bridge Abdul safehouse virgins money detonators Allah
smallpox Glaspie Springfield agent airport dispersal facility counterfeit
Dec 22 '05 #9

P: n/a
On Thu, 22 Dec 2005 01:01:57 +0000, Andy Hassall wrote:
This is disgusting! Andy, I'm disappointed.


In what way?


Andy, the proper way to do it is #!/path/to/php in the first line
Calling PHP explicitly doesn't conform the IGO (International Geeks
Organization) standard for aesthetics. As a geek, I must protest.
Merry Christmas and a happy New Year!

--
http://www.mgogala.com

Dec 22 '05 #10

P: n/a

Andy Hassall wrote:
On 21 Dec 2005 09:06:53 -0800, "lawrence k" <lk******@geocities.com> wrote:
However, I need for this script to get called every 15 minutes. So I
set it up as a cron job. And cron doesn't like calling this thing. Cron
is nice enough to send me an email with an error message. Every 15
minutes I get this:

/home/httpd/vhosts/autostratus.com/httpdocs/makeRss.php: line 1: ?php:
No such fil


You seem to be trying to run the .php file directly. This won't work; you
either need a #! line at the beginning, or you need to run it via PHP.

So either:

#!/path/to/php

as the first line, which will mess things up when it's run via the web, or
change your cron entry to:

*/15 * * * * /path/to/php /path/to/makeRss.php

... as I'm assuming it's currently something like:

*/15 * * * * /path/to/makeRss.php


Thanks so much for all your help. I took the second route you here
suggest. And I got it to work, for the most part. The RSS feeds are
being created. However, the script is not allowed to run chmod() when
it is called by cron, though it has no problem running chmod() when it
is called from the browser. Why would that be? Is the user different?
Is the user Apache when it is called from the browser, and PHP when
called as a shell script? If so, I assume PHP lacks the rights to
overwrite what Apache did?

It also says gethostbyaddr() is not working. I don't know why. It seems
to work when the script is called from the browser.
I'm getting this, which confirms success but also has error messages:

Content-type: text/html
X-Powered-By: PHP/4.3.2
Set-Cookie: machineId=2173643fecddb17b14e53376e348e98c; expires=Sun,
16-Apr-2006
23:00:40 GMT

<br />
<b>Warning</b>: gethostbyaddr(): Address is not a valid IPv4 or IPv6
address in
<b>/home/httpd/vhosts/autostratus.com/httpdocs/tagIndexLibrary.php</b>
on line <b>74</b><br />
<br />
<b>Warning</b>: chmod(): Operation not permitted in
<b>/home/httpd/vhosts/autostratus.com/httpdocs/tagIndexLibrary.php</b>
on line <b>1530</b><br />

<p><a href="rss/music.xml">We just created the file rss/music.xml</a>
<br />
<b>Warning</b>: chmod(): Operation not permitted in
<b>/home/httpd/vhosts/autostratus.com/httpdocs/tagIndexLibrary.php</b>
on line <b>1530</b><br />

<p><a href="rss/critical_mass_consciousness.xml">We just
created the file rss/critical_mass_consciousness.xml</a>
<br />
<b>Warning</b>: chmod(): Operation not permitted in
<b>/home/httpd/vhosts/autostratus.com/httpdocs/tagIndexLibrary.php</b>
on line <b>1530</b><br />

Dec 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.