How to run a PHP script on the server through the command line

I created a php script to import an rss feed into a database. The feed, which is huge (from 2004 to 2010, about 2 million records), must be inserted into the database. I run the script in the browser, but the pace that it inserts (about 1 time per second), I doubt that it will take another 20-25 days to enter this data, even if I run it 24 hours a day. I tried to work in different browsers at the same time and in the last two days I have finished only 70,000 entries. I am not sure how the server will react if I run 10-12 instances of it at the same time.

The programmer at my end of the client says that I can run it directly on the server through the command line. Can someone tell me what is the difference if I run it through the command line? And what is the command line syntax for running it? I am on apache, php / mysql. I tried over the Internet for a similar answer, but they seem rather confusing to me, since I'm not a system administrator or work well on Linux, although in the past I have performed tasks such as svn repositories and installing some Apache modules on the server, so I hope I could manage this if someone tells me how to do this.

+4
source share
4 answers

Speed โ€‹โ€‹difference: minimal. All you save is blocking NET I / O and connections (and apache overhead, which are negligible).

How to do it:

bash> php -f /path/to/my/php/script.php 

You may have the php5-mod package installed, which is php for apache, you may have to install the actual command line interpreter, however many distributions install both. Personally, I think you have a performance problem in the algorithm. Something that takes days and days seems to be accelerated by caching and analyzing performance in the worst case (Big-O note).

In addition, php vanilla is not very fast, there are many ways to do it very fast, but if you are doing heavy calculations you should consider c / C ++, C # / Mono (Maybe), maybe python (can be precompiled, actually can't be much faster).

But exploring these other remedies is highly recommended.

+8
source

It is enough to provide only the executable file name:

 php -f <YourScriptHere.php> 

See the documentation for additional command line options .

+3
source

To run php script on the command line, do only:

 php yourscript.php 

If you want this process to run in the background, follow these steps:

 php yourscript.php & 

Then you can start several processes at the same time. To identify the script instances that are currently running, run:

 ps aux | grep yourscript.php 

However, if you think this takes too much time, try to find out if there is any bottleneck in your code and optimize it.

+1
source

in linux:

 php -f file.php 

type

 php --help 

for other options

0
source

Source: https://habr.com/ru/post/1310892/


All Articles