php 如何通过命令行在服务器上运行php脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2907555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run php script on server through command line
提问by Arvind K.
I have created a php script to import rss feed into the database. The feed which is huge (from year 2004 to 2010, approx 2 million records) has to be inserted into the database. I have been running the script in browser but the pace it is inserting (approx. 1 per second) i doubt it takes another 20-25 days to input this data even if i run it 24 hrs a day. I have tried it running on different browser windows at the same time and have finished only 70000 records in last two days. I am not sure how the server would react if i run 10-12 instances of it simultaneously.
我创建了一个 php 脚本来将 rss 提要导入数据库。必须将庞大的提要(从 2004 年到 2010 年,大约 200 万条记录)插入到数据库中。我一直在浏览器中运行脚本,但它插入的速度(大约每秒 1 次)我怀疑即使我每天运行 24 小时也需要 20-25 天才能输入这些数据。我试过它同时在不同的浏览器窗口上运行,最近两天只完成了 70000 条记录。如果我同时运行 10-12 个实例,我不确定服务器会如何反应。
A programmer at my client's end says that i could run it directly on the server through command line. Could anyone tell me how much difference it would make if i run it through command line? Also what is the command line syntax to run it? I am on apache, php/mysql. I tried out over the web for a similar answer but they seem quite confusing to me as i am not a system administrator or that good in linux although i have done tasks like svn repositories and installing some apache modules on server in the past so i hope i could manage this if someone tell me how to do it.
我客户端的一个程序员说我可以通过命令行直接在服务器上运行它。谁能告诉我如果我通过命令行运行它会有多大区别?另外运行它的命令行语法是什么?我在 apache、php/mysql 上。我在网上尝试过类似的答案,但它们对我来说似乎很困惑,因为我不是系统管理员或在 linux 方面做得很好,尽管我过去做过 svn 存储库和在服务器上安装一些 apache 模块之类的任务,所以我希望如果有人告诉我怎么做,我可以做到这一点。
回答by Aren
Difference in speed: Minimal. All you save on is blocking on NET I/O and connection (and the apache overhead which is negligible).
速度差异:最小。您所节省的只是阻塞 NET I/O 和连接(以及可以忽略不计的 apache 开销)。
How to do it:
怎么做:
bash> php -f /path/to/my/php/script.php
You may only have the php5-modpackage installed which is php for apache, you may have to install the actual command line interpreter, however a lot of distros install both. Personally I think you have an efficiencyproblem in the algorithm. Something taking days and days seems like it could be sped up by caching & worst-case performance analysis (Big-O notation).
您可能只php5-mod安装了用于 apache 的 php 包,您可能需要安装实际的命令行解释器,但是很多发行版都安装了这两个包。我个人认为您在算法中存在效率问题。一些需要几天时间的事情似乎可以通过缓存和最坏情况性能分析(大 O 符号)来加速。
Also, php vanilla isn't very fast, there's lots of ways to make it really fast, but if you're doing heavy computation, you should consider c/c++, C#/Mono (Maybe), possibly python (can be pre-compiled, may not actually be much faster).
此外,php vanilla 不是很快,有很多方法可以让它变得非常快,但是如果你正在做大量计算,你应该考虑 c/c++、C#/Mono(也许),可能是 python(可以是 pre-编译,实际上可能不会快得多)。
But the exploration of these other outlets is highly recommended.
但强烈建议探索这些其他网点。
回答by Dolph
Only providing the filename to execute is sufficient:
只提供要执行的文件名就足够了:
php -f <YourScriptHere.php>
See the documentationfor more command line options.
回答by nuqqsa
To run a php script in the command line just execute:
要在命令行中运行 php 脚本,只需执行:
php yourscript.php
If you want to keep this process running in background do:
如果你想让这个进程在后台运行,请执行以下操作:
php yourscript.php &
You can then run several processes at the same time. To identify the instances of the script that are currently running execute:
然后,您可以同时运行多个进程。要识别当前正在运行的脚本实例,请执行:
ps aux | grep yourscript.php
However, if you think it takes too long, try to find out whether there's any bottleneck in your code and optimize it.
但是,如果您认为它花费的时间太长,请尝试找出您的代码中是否存在瓶颈并对其进行优化。
回答by David Crowe
You may also need the -noption (no php.inifile) or options to specify where php-cli.inior php.inifile can be found.
您可能还需要-n选项(无php.ini文件)或选项来指定可以找到php-cli.ini或php.ini文件的位置。
回答by Dan Heberden
in linux:
在Linux中:
php -f file.php
type
类型
php --help
for other options
对于其他选择

