netrox
netrox

Reputation: 5326

How do I know when the task from command line is finished?

This is really important as I could not find anything I am looking for in Google.

How do I know when the application (or is it more appropriate to call it a task?) executed by a command line is done? How does the PHP know if the task of copying several files are done if I do like this:

 exec("cp -R /test/ /var/test/test");

Does the PHP script continue to go to next code even while the command is still running in background to make copies? Or does PHP script wait until the copy is finished? And how does a command line application notify the script when it's done (if it does)? There must be some kind of interaction going on.

Upvotes: 1

Views: 2919

Answers (2)

Begeesy
Begeesy

Reputation: 101

php's exec returns a string so yes. Your webpage will freeze until the command is done. For example this simple code

<?PHP
echo exec("sleep 5; echo HI;");
?>

When executed it will appear as the page is loading for 5 seconds, then it will display:

 HI;

How does the PHP know if the task of copying several files are done if I do like this?

Php does not know, it simply just run the command and does not care if it worked or not but returns the string produced from this command. Thats why it better to use PHP's copy command because it returns TRUE/FALSE upon statistics. Or create a bash/sh script that will return 0/FALSE or 1/TRUE to determine if command was successful if you are going this route. Then you can PHP as such:

<?PHP
$answer = exec("yourScript folder folder2");
if ($answer=="1") {
//Plan A Worked
} else {
//Plan A FAILED try PlanB
}
?>

Upvotes: 1

arkascha
arkascha

Reputation: 42915

It waits until the exec call returns, whatever it returns.

However it might be that the exit call returns although the command it has started has not yet finished. That might be the case if you detach from the control, for example by explicitly specifying a "&" at the end of the command.

Upvotes: 0

Related Questions