?
Solved

Most effective way to execute PHP CLI scripts?

Posted on 2011-03-21
11
Medium Priority
?
565 Views
Last Modified: 2012-05-11
http://www.php-cli.com/php-cli-tutorial.shtml

The tutorial above says that it's ugly and ineffective to run a PHP CLI script like this:
#> /usr/bin/php /path/to/script/test.php

Open in new window

And that it's much better to code it like a bash script like this:
#!/usr/bin/php -q
<?php
echo "Hello world of PHP CLI!";
?>

Open in new window

And then run it like this:
#> chmod 755 /path/to/script/bash_formatted_test.php
#> ./path/to/script/bash_formatted_test.php

Open in new window


Why is the second way better?

Thanks,

http://www.t1shopper.com/

PS. The PHP tutorial doesn't indicate if there's a preferred method:
http://www.php.net/manual/en/features.commandline.usage.php
0
Comment
Question by:Geoff Millikan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35183687
If you are on Linux then have you considered using the Lynx browser? It saves you having to configure and use the CLI version and if it works in a normal browser then it works in Lynx as well. Scripts can be executed as

lynx -dump http://www.example.com/path/to/script.php
0
 
LVL 32

Expert Comment

by:DrDamnit
ID: 35183949
The second way is the preferred way of running the script because you're telling BASH to use the php cli interpreter in that first line.

The secondary part (changing file permissions) is required to make something executable.

I agree with the tutorial.
0
 

Author Comment

by:Geoff Millikan
ID: 35184043
bportlock: I don't need or want to use a web browser, we want to execute PHP as CLI.  For example, as a cron job.  Any insight on the question at hand?

DrDamnit: Why is that better?  Is it because it's "cleaner and more straightforward" or is there some efficiency to be gained in the form of a more direct path to the binary which results in less system bus usage, or maybe less RAM used, etc ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 32

Assisted Solution

by:DrDamnit
DrDamnit earned 600 total points
ID: 35184246
"cleaner and more straightforward". Exactly.

Plus, you don't have to worry about fat-fingering arguments to the php command line.

I have extremely complicated PHP scripts that also launch MP3 encoders to grab calls when they interface with my phone system, then it queries databases, and other bash scripts scripts all running off that style of php script.
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 1400 total points
ID: 35184292
"we want to execute PHP as CLI.  For example, as a cron job.  Any insight on the question at hand?"

I execute my scripts via cron using Lynx, for example

* 0,6,12,18 * * * lynx -dump http://www.example.com/script.php?param=1234

I've never found CLI to be worth the bother. Also I endorse Dr Damnit's comments above about replacing the shebang line with #!/usr/bin/php -q
0
 
LVL 32

Expert Comment

by:DrDamnit
ID: 35184317
I also endorse bportlock's lynx trick. Never though of that, but it will work just the same.

6 in one hand, half a dozen in the other.

The only advantage you have with CLI is if you want to write to the screen while running a script. I am OCD with outputs, so I like to watch the pretty letters scream by....

opening file SOMEFILE....[OK]
Sending email to drdamnit@awesomeness.com....[OK]


and that jazz.

But if you don't need / want that, either way is sufficient.
0
 

Author Comment

by:Geoff Millikan
ID: 35190154
Very interesting.  Using lynx has the advantage of caching content (if that is desired) but has the disadvantage of using up extra overhead of an Apache process.  Executing the PHP script via CLI SAPI means no Apache resources are consumed which seems a bit more efficient unless I'm missing something?

So it's no more effective (at least when executing CLI scripts) to run them one way over another.  The different styles are merely presentational preferences, right?
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 1400 total points
ID: 35191014
"Executing the PHP script via CLI SAPI means no Apache resources are consumed which seems a bit more efficient unless I'm missing something?

Running the CLI-PHP is not resource free either, no matter how you run it it will require resources. Unless your machine is very, very tight on its resource allocation then it will not make much difference which way you do this. I certainly wouldn't worry about the machine complaining or wanting extra days off......


"So it's no more effective (at least when executing CLI scripts) to run them one way over another.  The different styles are merely presentational preferences, right?"

To an extent.... the major difference is that using Lynx you need only ever worry about the one php.ini file and the system is constrained to work within Apache's framework and constraints. In short you do not have to worry about setting up, maintaining and testing TWO environments. It also means that the job runs as www / www-data / wwrun / apache with all the permissions and access restrictions that that implies. The big plus is that if the script works in FireFox or IE I know it will run under Lynx, but if I run it under CLI then some error (usually permissions or a missing extension not enabled in CLI) can stop the script dead in its tracks. Using Lynx makes my life easier.

It is a choice and my preference is to keep PHP boxed up inside the webroot as much as possible so I never enable CLI.
0
 

Author Comment

by:Geoff Millikan
ID: 35363007
...you need only ever worry about the one php.ini file...

I thought there was only one php.ini file which was always used?  Are you saying I can specify one for the web server (libphp5.so) and another for CLI?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35373376
" Are you saying I can specify one for the web server (libphp5.so) and another for CLI?"

Yes you can do exactly that. The -c swith on the PHP command lets you specify the php.ini to use.

http://www.php.net/manual/en/features.commandline.options.php (scan for --php-ini )
0
 

Author Comment

by:Geoff Millikan
ID: 35438325
Ah, that has a distinct advantage in that now I can configure my php-apache.ini file a lot smaller than my php-cli.ini file.  For example, I'm reducing the amount of memory and the execution time in php-apache.ini and in increasing those things in php-cli.ini.

Very nice.
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question