Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 21st, 2005, 05:15 PM
lawrence k
Guest
 
Posts: n/a
Default a PHP script works fine when called from the browser but not as a shell script

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?

  #2  
Old December 21st, 2005, 05:55 PM
Andy Hassall
Guest
 
Posts: n/a
Default Re: a PHP script works fine when called from the browser but not as a shell script

On 21 Dec 2005 09:06:53 -0800, "lawrence k" <lkrubner@geocities.com> wrote:
[color=blue]
>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[/color]

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 :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
  #3  
Old December 21st, 2005, 07:45 PM
Lars Eighner
Guest
 
Posts: n/a
Default Re: a PHP script works fine when called from the browser but not as a shell script

In our last episode,
<1135184813.363991.89850@g49g2000cwa.googlegroups. com>,
the lovely and talented lawrence k
broadcast on comp.lang.php:
[color=blue]
> /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");[/color]

[color=blue]
> Why would cron have a problem if the script works fine from the web
> browser?[/color]

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 usenet@larseighner.com http://www.larseighner.com/
My mail reader can beat up your mail reader.
  #4  
Old December 21st, 2005, 11:45 PM
lawrence k
Guest
 
Posts: n/a
Default Re: a PHP script works fine when called from the browser but not as a shell script


Andy Hassall wrote:[color=blue]
> On 21 Dec 2005 09:06:53 -0800, "lawrence k" <lkrubner@geocities.com> wrote:
>[color=green]
> >
> >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[/color]
>
> 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[/color]

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

  #5  
Old December 22nd, 2005, 12:15 AM
Andy Hassall
Guest
 
Posts: n/a
Default Re: a PHP script works fine when called from the browser but not as a shell script

On 21 Dec 2005 15:32:30 -0800, "lawrence k" <lkrubner@geocities.com> wrote:
[color=blue]
>Andy Hassall wrote:[color=green]
>> On 21 Dec 2005 09:06:53 -0800, "lawrence k" <lkrubner@geocities.com> wrote:
>>[color=darkred]
>> >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[/color]
>>
>> 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[/color]
>
>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[/color]

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 :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
  #6  
Old December 22nd, 2005, 01:05 AM
Mladen Gogala
Guest
 
Posts: n/a
Default Re: a PHP script works fine when called from the browser but not as a shell script

On Wed, 21 Dec 2005 09:06:53 -0800, lawrence k wrote:
[color=blue]
> Why would cron have a problem if the script works fine from the web
> browser?[/color]

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

  #7  
Old December 22nd, 2005, 01:05 AM
Mladen Gogala
Guest
 
Posts: n/a
Default Re: a PHP script works fine when called from the browser but not as a shell script

On Wed, 21 Dec 2005 17:42:34 +0000, Andy Hassall wrote:
[color=blue]
> */15 * * * * /path/to/php /path/to/makeRss.php[/color]

This is disgusting! Andy, I'm disappointed.

--
http://www.mgogala.com

  #8  
Old December 22nd, 2005, 01:15 AM
Andy Hassall
Guest
 
Posts: n/a
Default Re: a PHP script works fine when called from the browser but not as a shell script

On Thu, 22 Dec 2005 00:51:16 GMT, Mladen Gogala <gogala@sbcglobal.net> wrote:
[color=blue]
>On Wed, 21 Dec 2005 17:42:34 +0000, Andy Hassall wrote:
>[color=green]
>> */15 * * * * /path/to/php /path/to/makeRss.php[/color]
>
>This is disgusting! Andy, I'm disappointed.[/color]

In what way?

--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
  #9  
Old December 22nd, 2005, 01:15 AM
Lars Eighner
Guest
 
Posts: n/a
Default Re: a PHP script works fine when called from the browser but not as a shell script

In our last episode,
<1135207950.627575.190780@g49g2000cwa.googlegroups .com>, the
lovely and talented lawrence k broadcast on comp.lang.php:

[color=blue]
> Andy Hassall wrote:[color=green]
>> On 21 Dec 2005 09:06:53 -0800, "lawrence k" <lkrubner@geocities.com> wrote:
>>[color=darkred]
>> >
>> >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[/color]
>>
>> 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[/color][/color]
[color=blue]
> I ran phpinfo() to find out the path, and this is the info I got:[/color]
[color=blue]
> PATH /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin[/color]
[color=blue]
> _SERVER["PATH"] /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin[/color]
[color=blue]
> _SERVER["PATH_TRANSLATED"] /home/httpd/vhosts/autostratus.com/httpdocs/info.php[/color]
[color=blue]
> _ENV["PATH"] /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin[/color]

[color=blue]
> 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:[/color]
[color=blue]
> #!/sbin/php[/color]

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 usenet@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
  #10  
Old December 22nd, 2005, 04:25 AM
Mladen Gogala
Guest
 
Posts: n/a
Default Re: a PHP script works fine when called from the browser but not as a shell script

On Thu, 22 Dec 2005 01:01:57 +0000, Andy Hassall wrote:
[color=blue][color=green]
>>This is disgusting! Andy, I'm disappointed.[/color]
>
> In what way?[/color]

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

  #11  
Old December 22nd, 2005, 05:45 AM
lawrence k
Guest
 
Posts: n/a
Default Re: a PHP script works fine when called from the browser but not as a shell script


Andy Hassall wrote:[color=blue]
> On 21 Dec 2005 09:06:53 -0800, "lawrence k" <lkrubner@geocities.com> wrote:[color=green]
> >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[/color]
>
> 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[/color]

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles