Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 24 Jul 2014, 19:23
All times are UTC - 4
 Forum index » House Training » Users ( For the regulars )
How to run php scripts from command line ? [solved]
Moderators: Flash, Ian, JohnMurga
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count  
Author Message
Robotop


Joined: 08 Dec 2009
Posts: 34
Location: Italy

PostPosted: Mon 01 Apr 2013, 14:15    Post_subject:  How to run php scripts from command line ? [solved]  

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:

##!/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 Smile

Edited_time_total
Back to top
View user's profile Send_private_message Visit_website 
L18L

Joined: 19 Jun 2010
Posts: 2504
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Mon 01 Apr 2013, 15:32    Post_subject:  

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
Back to top
View user's profile Send_private_message 
Robotop


Joined: 08 Dec 2009
Posts: 34
Location: Italy

PostPosted: Tue 02 Apr 2013, 09:56    Post_subject:  

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 Smile ). May be this different package has missing php command line interpreter ?
Edited_time_total
Back to top
View user's profile Send_private_message Visit_website 
L18L

Joined: 19 Jun 2010
Posts: 2504
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Tue 02 Apr 2013, 10:32    Post_subject: How to run php scripts from command line ?  

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/it/xampp-linux.html
and then
#!/opt/lampp/bin/php
will work.
Back to top
View user's profile Send_private_message 
npierce

Joined: 28 Dec 2009
Posts: 858

PostPosted: Tue 02 Apr 2013, 10:35    Post_subject:  

Robotop wrote:
Code:
##!/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:
which php
Back to top
View user's profile Send_private_message 
Robotop


Joined: 08 Dec 2009
Posts: 34
Location: Italy

PostPosted: Tue 02 Apr 2013, 10:40    Post_subject:  

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 Smile

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

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

Re-edited: Gulp ! Xampp is 81 Mb download... is there some "lighter" solution ?

Edited_time_total
Back to top
View user's profile Send_private_message Visit_website 
L18L

Joined: 19 Jun 2010
Posts: 2504
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Tue 02 Apr 2013, 10:44    Post_subject:  

npierce wrote:
...Your #! line really needs to be the first line in the file.....


Thus no comment in 1st line.
Retry please Cool

--
edit

Code:
which php-cgi
Question
Back to top
View user's profile Send_private_message 
Robotop


Joined: 08 Dec 2009
Posts: 34
Location: Italy

PostPosted: Tue 02 Apr 2013, 10:56    Post_subject:  

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 Smile

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-
Back to top
View user's profile Send_private_message Visit_website 
L18L

Joined: 19 Jun 2010
Posts: 2504
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Tue 02 Apr 2013, 11:46    Post_subject:  

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 Laughing
Back to top
View user's profile Send_private_message 
Robotop


Joined: 08 Dec 2009
Posts: 34
Location: Italy

PostPosted: Tue 02 Apr 2013, 12:00    Post_subject:  

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 Smile

-edited- just to be more clear:
Code:

#!/usr/local/bin/php-cgi -c /etc
<?php
...
?>
Back to top
View user's profile Send_private_message Visit_website 
npierce

Joined: 28 Dec 2009
Posts: 858

PostPosted: Sat 06 Apr 2013, 08:26    Post_subject:  

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:
##!/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:
#!/usr/local/bin/php
<?php
  print "Test PHP script with #! on top line."
?>

test2.php:
Code:
## 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:
# ./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:
#!/bin/sh
function send_message() {
  echo "Message: $MSG"
}
MSG="Hi." send_message
echo "Final value of MSG: $MSG"

test2.sh
Code:
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:
# ./test1.sh
Message: Hi.
Final value of MSG: Hi.
#
# ./test2.sh
Message: Hi.
Final value of MSG:
#
Back to top
View user's profile Send_private_message 
Robotop


Joined: 08 Dec 2009
Posts: 34
Location: Italy

PostPosted: Sat 06 Apr 2013, 08:42    Post_subject:  

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 Smile
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 Smile
Sorry for misunderstandings Sad
Back to top
View user's profile Send_private_message Visit_website 
npierce

Joined: 28 Dec 2009
Posts: 858

PostPosted: Sat 06 Apr 2013, 09:00    Post_subject:  

Ah, OK, now I understand. Smile
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » House Training » Users ( For the regulars )
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1005s ][ Queries: 11 (0.0029s) ][ GZIP on ]