How to run php scripts from command line ? [solved]

Using applications, configuring, problems
Post Reply
Message
Author
User avatar
Robotop
Posts: 34
Joined: Tue 08 Dec 2009, 14:05
Location: Italy
Contact:

How to run php scripts from command line ? [solved]

#1 Post by Robotop »

Hello, I have a running LAMP on Puppy 4.3.1 ; all works fine, serving php pages quickly and efficiently. Now, I want to run some php scripts from command line (or in a rxvt terminal), but the "php-cli" , that I suppose to be the handler for such scripts, doesn't exists on disk, on my actual installation.
Anyway, I tried with php-cgi or php-fcgi, but without success.
This is an example of what I want to run (execution rights given):

Code: Select all

##!/usr/local/bin/php-cgi -h
#!/usr/sbin/php-fcgi -h 
<?php
  $dtime = 5;
  print "press Ctrl-C to stop\n";
  while(1)
    {
    print "Time: " . date("%r") . "\n";
    sleep($dtime);
    }
?>
Is there some .pet package for enabling the use of php in this way, outside a webpage and while the LAMP is running (even serving php pages) ?
Any help will be greatly appreciated :)
Last edited by Robotop on Tue 02 Apr 2013, 15:58, edited 1 time in total.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#2 Post by L18L »

Got it run
my console wrote:# ./php
press Ctrl-C to stop
Time: %Mon, 01 Apr 2013 20:34:19 +0200
Time: %Mon, 01 Apr 2013 20:34:24 +0200
Time: %Mon, 01 Apr 2013 20:34:29 +0200
Time: %Mon, 01 Apr 2013 20:34:34 +0200
^C
#
my /root/my-applications/bin/php wrote:#!/opt/lampp/bin/php
<?php
$dtime = 5;
print "press Ctrl-C to stop\n";
while(1)
{
print "Time: " . date("%r") . "\n";
sleep($dtime);
}
?>
:wink:

User avatar
Robotop
Posts: 34
Joined: Tue 08 Dec 2009, 14:05
Location: Italy
Contact:

#3 Post by Robotop »

Hello LIBL, thank you for the prompt answer. I looked into my file system and under the directory /opt I just have another subdirectory named "samba" and not any "lampp". May be my installation is different from yours. I don't have Apache, but Hiawatha (as web server) in my LAMP pet (so, the package "true name" should be something like LHMP :) ). May be this different package has missing php command line interpreter ?
Last edited by Robotop on Tue 02 Apr 2013, 14:33, edited 1 time in total.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

How to run php scripts from command line ?

#4 Post by L18L »

Sure we have something different.
My lampp ist LinuxApacheMysqPhpPackage from Xampp
downloaded and expanded into /opt which is a symlink to /mnt/home/opt.
Thus it can be used in any linux that has access to /mnt/home.

But it cannot be the different web server because the web server is not running.

Maybe try with simple
#!/usr/local/bin/php
or
#!/usr/sbin/php-fcgi

not
##!/usr/local/bin/php

Just a guess.

Or use lampp from
http://www.apachefriends.org/[b]it[/b]/xampp-linux.html
and then
#!/opt/lampp/bin/php
will work.

npierce
Posts: 858
Joined: Tue 29 Dec 2009, 01:40

#5 Post by npierce »

Robotop wrote:

Code: Select all

##!/usr/local/bin/php-cgi -h
#!/usr/sbin/php-fcgi -h
Your #! line really needs to be the first line in the file.

You can find you php executable with:

Code: Select all

which php

User avatar
Robotop
Posts: 34
Joined: Tue 08 Dec 2009, 14:05
Location: Italy
Contact:

#6 Post by Robotop »

Hello, the reason of double ## is that this one is only a remark.
I tried both php-cgi in its proper directory and php-fcgi in the other one (you can see the path in first lines of my php sources), having troubles.
I think that my LHMP has only one interpreter for php scripts embedded in webpages, not for command line.
I will look at the link you proposed to see if it can solve my problem.
Thanks :)

Edited: ... and 'which php' gives no results ! I tried all the "normal" operations before asking on the forum :)

Anyways, php embedded in web pages works fine...

Re-edited: Gulp ! Xampp is 81 Mb download... is there some "lighter" solution ?
Last edited by Robotop on Tue 02 Apr 2013, 14:47, edited 1 time in total.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#7 Post by L18L »

npierce wrote:...Your #! line really needs to be the first line in the file.....
Thus no comment in 1st line.
Retry please 8)

--
edit

Code: Select all

which php-cgi
:?:

User avatar
Robotop
Posts: 34
Joined: Tue 08 Dec 2009, 14:05
Location: Italy
Contact:

#8 Post by Robotop »

Hello L18L (sorry, I did a mistake on your nickname in previous post). Obviously, the which command gives the path you can read in my source. Both php-cgi and php-fcgi are present in different directories. One of them gives some errors, the other gives different ones. For example, the php-fcgi gives errors starting with the php header (the lt sign followed by the question mark), then gives error on instruction "print" and others, while the php-cgi gives a great number of errors, cause can't find the path for various libraries. I think it simply needs some "parser" to redirect the input/output that's usually a container like the web page.
Anyway, I'm just looking for a single "pet" that gives me this interpreter running in some Mb... in other distributions, the file is called php-cli , just to say that is php with command line interface.
Thanks for your help :)

Edited: may be I didn't explain myself about the remarks... I posted the first 2 source lines here just to show where the applications are located on my system; the double ## is just to say: -this is a remark-

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#9 Post by L18L »

Robotop wrote:...Anyway, I'm just looking for a single "pet" that gives me this interpreter running in some Mb... in other distributions, the file is called php-cli , just to say that is php with command line interface....
Yes that makes sense.
cgi is something for a server.
...and Xampp is a monster.

LIBL :lol:

User avatar
Robotop
Posts: 34
Joined: Tue 08 Dec 2009, 14:05
Location: Italy
Contact:

#10 Post by Robotop »

OK, solved. I just added the parameter -c /etc at the first line of script (calling php-cgi) for indicating WHERE the php.ini file resides. Et voilà it works now :)

-edited- just to be more clear:

Code: Select all

#!/usr/local/bin/php-cgi -c /etc 
<?php
...
?>

npierce
Posts: 858
Joined: Tue 29 Dec 2009, 01:40

#11 Post by npierce »

Robotop,

Good to know that you got your code working.
Robotop wrote:. . . the reason of double ## is that this one is only a remark.
Yes, it was clear that you wanted to "comment-out" that line. But the first line of any interpreted script is special. Yes, to PHP, that line was a comment. But that is also true of a proper #! line. That line is not intended for the PHP interpreter, it is intended for the execve() system call, which is how one program in Linux executes another. And the reason that the #! line starts with a # is to make the line a comment, since it should be ignored by the interpreter (which, in this case, is a PHP interpreter).
Robotop wrote:

Code: Select all

##!/usr/local/bin/php-cgi -h
#!/usr/sbin/php-fcgi -h 
. . .
Robotop wrote:For example, the php-fcgi gives errors starting with the php header (the lt sign followed by the question mark), then gives error on instruction "print" and others . . .
Yes, that will happen when bash tries to interpret a script meant for php, not bash, which it will do if the script doesn't have a proper #! line as the first line in the file. Although you were trying to use php-fcgi, php-fcgi was never run because there was no valid #! line, so bash assumed it was a bash script and spit out errors because it wasn't proper bash code.

(In this case, as you have already discovered, even if you had removed the comment in the first line so that the script had a proper #! line at the top, your PHP code still wouldn't have been properly interpreted. It would have caused php-fcgi to be executed, but the php-fcgi executable isn't a PHP interpreter (it just starts the FastCGI daemon). So you still would have gotten error messages, although the error messages from php-fcgi would have been different from those that you got when bash tried to interpret the script.)

Consider the following two scripts. The first has a proper #! line at the top of the file. The second has a comment at the top, and the #! line below the comment will be ignored.

test1.php:

Code: Select all

#!/usr/local/bin/php
<?php
  print "Test PHP script with #! on top line."
?>
test2.php:

Code: Select all

## Comment on top line.
#!/usr/local/bin/php
<?php
  print "Test PHP script with comment on top line."
?>
Here's what happens when we run them:

Code: Select all

# ./test1.php
Test PHP script with #! on top line.
# 
# ./test2.php
./test2.php: line 3: ?php: No such file or directory
./test2.php: line 4: print: command not found
./test2.php: line 5: syntax error near unexpected token `newline'
./test2.php: line 5: `?>'
# 
Here is what the man page for the execve() Linux system call says:
man page for execve wrote:Interpreter scripts

An interpreter script is a text file that has execute permission enabled and whose first line is of the form:

#! interpreter [optional-arg]

Your question was related to PHP, so the remainder of this post is somewhat off-topic since it applies to shell scripts, not PHP scripts. So, if you don't write shell scripts, skip it if you like. I add it only because it relates to the use of a #! line.


Although bash assumes that any script file that doesn't start with a valid #! line is a shell script, it is not a good idea to leave it out even for shell scripts. By having a proper #! line, you can be sure that the script is interpreted by the proper shell, even if it is invoked by another shell.

For instance, you might not want a shell script that was written specifically for the Bourne shell to be interpreted by bash.

Even though Puppies don't come with Bourne shells, and /bin/sh is usually just a link to /bin/bash, bash will behave a little differently when invoked with sh instead of bash. For instance, when invoked as bash, a special environment file is used if the BASH_ENV variable is defined; this is not the case when bash is invoked as sh. Another difference (among many others) is that lines with assignment statements preceding shell function calls are handled differently.

Consider the following two scripts. One has a #! line that indicates it should be interpreted by /bin/sh. The other has no #! line, so bash will assume it is a bash script and it will be run by /bin/bash. Other than the first line, both files are the same.

test1.sh:

Code: Select all

#!/bin/sh
function send_message() {
  echo "Message: $MSG"
}
MSG="Hi." send_message
echo "Final value of MSG: $MSG"
test2.sh

Code: Select all

function send_message() {
  echo "Message: $MSG"
}
MSG="Hi." send_message
echo "Final value of MSG: $MSG"
Here is the result when run from bash:

Code: Select all

# ./test1.sh
Message: Hi.
Final value of MSG: Hi.
#
# ./test2.sh
Message: Hi.
Final value of MSG: 
# 

User avatar
Robotop
Posts: 34
Joined: Tue 08 Dec 2009, 14:05
Location: Italy
Contact:

#12 Post by Robotop »

Hello, probably my bad english is the reason for this long misunderstanding. The "code" section of my example, at the top of this thread, has the first line with the double ##, but this is NOT the script I run on my PC. My "real" script has ONLY one shebang line #! as first row. The remark is just to show both the locations where php-cgi and php-fcgi are stored on my system. probably, it would be better if I put such location infos it at the END of the script, just to not "offend" so many persons with a remark on the first line :)
The behaviour of the script using ONLY ONE #! line, alternatively with php-cgi and php-fcgi, is the one I described in last post before the solution.
Hope this time I was able to explain myself :)
Sorry for misunderstandings :(

npierce
Posts: 858
Joined: Tue 29 Dec 2009, 01:40

#13 Post by npierce »

Ah, OK, now I understand. :)

Post Reply