1

i am executing long running shell commands using PHP. in a single php file i am calling a series of shell_exec function to execute long runnable commands.

What's the problem is ,that i am getting a response before those shell commands executed. I think those shell_exec calls are queuing and running in background.

What i need is php should not send me a response until completely execute a all shell commands.

I tried both exec and shell_exec function but same thing is happening.

10
  • That is not the case. Neither exec nor shell_exec are asynchronous. Try it yourself. Run exec("sleep 15"); and see how long it takes to finish. Commented Apr 12, 2016 at 14:49
  • i tried executing exec("sleep 15"); its exactly working. i will wait for 15 seconds and then php is returning its response. but in my case its not working, PHP initiates all the shell_commands and its returning response, its not waiting for those for completion. i dont know whats the reason. Commented Apr 12, 2016 at 15:29
  • Well it sounds like the problem is with your commands rather than your PHP code then. Commented Apr 12, 2016 at 15:34
  • i am executing commands like "adb install" and "adb push". is there any way to control the shell commands execution in PHP like...creating own process and execute shell commands within it and wait for it to complete are like that. Commented Apr 12, 2016 at 15:36
  • Are you sure the commands are actually succeeding? What is the exit code? Commented Apr 12, 2016 at 15:37

2 Answers 2

0

You could go with output buffering http://php.net/manual/en/function.ob-start.php

So first you would call:

ob_start();

and once you're done with all your processes you'd call

ob_end_flush();

Provided you know the exec script has finished, either through a callback or sync.

Edit: This seems to be related to your processes, and might be a better way to spawn external processes as you get a descriptor for the async:

PHP shell_exec update output as script is running

Sign up to request clarification or add additional context in comments.

Comments

0

shell_exec waits for command, so, problem is, as u think, you're calling it into background, maybe a nohup?.

Also, de ob start, should doesn't work, for the same reason.

The only solution, i see, is to debug/modify your commands called via shell_exec and ensure doesn't run into background.

3 Comments

Also, maybe is a simple error, like you're executing somethin like shell_exec("wget www.web.com?param1=2&param2=x"); this causes a wget get into background (and with a wrong url), because the &
Without the exact commands you're running, i can't help more, sorry, i'm just guessing :)
i am executing commands like "adb install" and "adb push" etc...i am calling php script from andoird application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.