2

I'm manually running a PHP database-maintenance script which takes around 10-15 minutes to complete. If my computer loses web connection during this time, is the script always fully completed on the server or does it stop somewhere along the way?

If it does stop, is there anything I can do in PHP or apache config to prevent this?

cronoklee
  • 6,482
  • 9
  • 52
  • 80
  • script would be completed on the server. – mehulmpt Feb 25 '17 at 14:13
  • If the script is only maintenance, so eg backup, then send backup throught FTP and so on, maybe it would be easier to have a loot at a server script that you can run using CRON. I started to back-up my MariaDB using PHP and I migrate to shell script and CRON and that's perfect. See here at starting point: http://stackoverflow.com/questions/19664893/linux-shell-script-for-database-backup – Peter Feb 25 '17 at 14:56
  • Yes I use Cron normally but on rare occasions it is necessary to run these big scripts manually – cronoklee Feb 25 '17 at 14:58

3 Answers3

0

In a simple HTTP request, once your browser makes a request to server, the client now waits for server to respond. Even if you close the browser, and the server is executing, say, an OS command, it doesn't matter. Server will keep doing so, because server had to respond to client. Its not always connected to client (not talking about websockets here).

So its safe to assume that your task would not be killed on server even if you close your browser after sending request to server to perform a backup.

mehulmpt
  • 15,861
  • 12
  • 48
  • 88
0

Have a look at: http://php.net/manual/en/function.ignore-user-abort.php

When running PHP as a command line script, and the script's tty goes away without the script being terminated, then the script will die the next time it tries to write anything unless ignore_user_abort is set to TRUE.

You can also start you script via a CronJob. This might be the better solution if this task is recurrent.

But in both solutions, Use set_time_limit() to be sure that your max_execution_time does not cancel the execution of your script.

cronoklee
  • 6,482
  • 9
  • 52
  • 80
Luca Jung
  • 1,440
  • 11
  • 25
  • I'm going to assume this applies to browser scripts as well as "command line scripts" and mark this answer as correct. Thanks to all for input – cronoklee Feb 25 '17 at 14:54
0

There are a number of factors contributing to the execution of a script. You browser (as a HTTP client) sends a HTTP Request of some sort; the server receives the request, looks for the requested file (nothing than a file can ever be requested on the world), and starts executing it. That's it! Your browser does nothing regarding the management of the execution of the script, it just needs a response of some sort. Execution would be active regardless of the connection of the server with the client. Step 1: Your browser sends a HTTP request to the server requesting the execution of a file.

Step 2:The server starts the task of execution. If your script sends back a request within a proper time, the browser receives it, else, the browser throws time-out error/message.

Step 3: The server finishes the execution of the script. If there are any output resulting from the execution that must be send back to the browser, the response reaches the destination only if there is a live connection.

Note: Such performance-heavy, time-consuming tasks must be handled, managed and organized in a much more different and efficient way.

Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105