Good evening.

Often you have to write scripts in php (one-time), transfer data from one site to another, parsing, etc. The execution of the script can take 30-100 minutes. Naturally launching the script through a normal browser, accessing the script via http, the server returns 503 after some time (30, 60, 120) seconds, terminates the connection. Different server settings do not help.

I read that help in such cases, write php scripts under the console. The so-called php cli.

Can you please tell me how to do this? Suppose I have hosting with scripts, my next steps? I can not figure out where to open this console, etc. As I understand it, php cli is just such an interpreter launch mode, with some parameters.

  • about 503. Most likely, all from the fact that the browser does not receive any information. they have their own limitations on how to get a response. if on ajax, you can set the wait time, then you need to do something on the server with php. when I wrote parsers, I was deceiving them (browsers), giving the browser at least something. (for example print ''; \ flush ()) thus the browser receives the output and does not close the connection. just do not forget about the gap itself. if it occurs, execution may fail. - noadev
  • Do not forget to specify max_exec ~ _time \ max_memory_limit \ to optimize the script itself. in the case of one apache, I did not notice the disconnection of the connection; in the case when nginx is a proxy, we don’t forget about all sorts of proxy_send_timeout \ and settings not to interrupt the execution when the connection is broken. unfortunately I don’t remember all the settings from memory, so I could not write the names correctly either. I hope something useful managed to share. and I advise you to write asynchronous applications, possibly using other technologies such as nodejs, which will increase the speed of your parsers - noadev

2 answers 2

Suppose you have a Windows computer. You can open the console and type some commands there. type dir and get a list of directories and files, copy files with the command copy : copy c:\a.txt d:\b.txt

this is a windows console. If php is installed on your computer, you can open the console, type something like c:\lamp\php\bin\php -i and see the php settings.

Now imagine your hosting. An operating system, not Windows, but Linux. Most likely it does not have a graphical shell. Your only way to communicate with him is through the command line.

PHP CLI stands for this: Command Line Interface. Command line interface

If you have ssh access to your site, you can use it to run php scripts in the console, create other console magic.

Log in via ssh to your host (ssh example_user@example.org), get into the console and run php ваш_скрипт.php

The main differences between php-cli and "php via browser":

1) php-cli is executed with the rights of the user who starts it, php-through-browser is executed with the rights of the "user" of the web server.

2) You can run something in the spirit of sudo php someFile.php and execute it with root privileges (the most important user in the system)

3) In php-cli, by default there is no limit on the execution time of the script.

If you, for example, have forgotten how php works in principle - let me remind you. your server (apache, nginx) receives a request — let's say at index.php — the server sends this request to the php-fpm daemon, which executes the index.php file, and sends the results to the server, and the server to your browser.

It's as if you received a letter from a friend asking you to log in to the console, execute some kind of php script and send the result of the execution back.

Only much faster.

In principle, this is how interpreters of programming languages ​​work, in fact, via the console. Web server support is completely optional.

Details and settings you can find in the manual PHP.

    Find your hosting SSH settings (as a rule, free hosting does not give access, or limit it with a virtual console or php-cgi and not php-cli, if you are lucky and you have access then connect via Putty (Windows) , or simply dial ssh and connection settings in a Linux terminal)

    Then simply call the desired script from this console.

    enter image description here PS I recommend to make scripts similar to the above root of the site, so that they could not run from the browser and through the console, you still get to them