0

I am working HTML and PHP for the first time. I have a php page that has a submit button which calls a shell script. This shell script performs some tasks which takes about 10-15 mins. When the submit button is clicked, a new page opens which prints some info immediately and then runs that script in the background.

However the browser tab shows the page to still be loading. Is there anyway I can stop the loading bar. The php code snippet looks something like this:

if($_POST['action'] == 'Test') {
    $result = liveExecuteCommand("/usr/bin/test.sh &");
}
Philipp
  • 15,377
  • 4
  • 35
  • 52
vedang09
  • 59
  • 1
  • 7
  • cal the script once the page is loaded not immediately or you can use die() function to stop that page – Umar Majeed Jan 05 '18 at 09:23
  • Didnt get you? Can you elaborate? – vedang09 Jan 05 '18 at 09:24
  • What is `liveExecuteCommand` supposed to do? – Philipp Jan 05 '18 at 09:26
  • $proc = popen("$cmd 2>&1 ;", 'r'); $live_output = ""; $complete_output = ""; while (!feof($proc)) { $live_output = fread($proc, 4096); $complete_output = $complete_output . $live_output; echo "$live_output"; @ flush(); } – vedang09 Jan 05 '18 at 09:37
  • Possible duplicate of [Continue PHP execution after sending HTTP response](https://stackoverflow.com/questions/3833013/continue-php-execution-after-sending-http-response) – dferenc Jan 05 '18 at 17:36
  • You will find this answer interesting: https://stackoverflow.com/a/14469376/3897122 – dferenc Jan 05 '18 at 17:36

2 Answers2

1

Use the exec command and redirect the output to null:

if ($_POST['action'] == 'Test') {
    exec("/usr/bin/test.sh > /dev/null &");
}

This should not wait for the process to finish.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • Do I need to include something to be able to use the exec command? It seems to be throwing some error if I directly use it – vedang09 Jan 05 '18 at 09:33
  • @VedangGutgutia no - execute is and build in function. If you run it on some shared hosts, it might be blocked (but in this case, others shouldn't also work) – Philipp Jan 05 '18 at 09:35
  • @Philipp it doesnt seem to be working for me. Any other way I can go about this? – vedang09 Jan 05 '18 at 09:51
  • If SELinux is working on your machine you should put it into permissive mode. Or find a way to let php execute commands with SELinux. – jeprubio Jan 05 '18 at 10:04
1

Destructors are executed even if the script gets terminated using die() or exit().

For More Details :
exit
how to stop

A.D.
  • 2,352
  • 2
  • 15
  • 25
  • It seems, the interpreter stucks in `liveExecuteCommand`. Putting an exit before it, wouldn't execute the script at all and putting it after wouldn't make it better – Philipp Jan 05 '18 at 09:28
  • Yep supposed ;) – A.D. Jan 05 '18 at 09:29