donald.sys
donald.sys

Reputation: 301

How to check bash shell finished in php

I am having difficulty processing bash shell in php. My problem is as below. I have 3 tasks corresponding to 3 files bash shell (task1.sh, task2.sh, task3.sh). When task1.sh finishes processing, task2.sh will automatically execute, when task2.sh finishes processing, task3.sh will automatically execute.

Initially, I wrote a file called task.sh and embedded task1.sh, task2.sh, task3.sh into it. But I want to embed these 3 tasks into a php file.

For example: I create task.php and do the following:

All 3 tasks I want to run backgroud. The problem is that when I run the background bash shell, I will not be able to check the failed error statement (the result always returns to 0).

I have learned a lot and consulted many documents but it did not help me.

I hope you can support me.

Sorry, my english very poor.

Upvotes: 1

Views: 419

Answers (2)

Koala Yeung
Koala Yeung

Reputation: 7843

You may use the $retval argument of exec().

<?php

exec('task1.sh', $output, $retval);
if ($retval !== 0) {
  // task 1 failed
  exit('Error running task1: ' . implode("<br/>\n", $output));
}

exec('task2.sh', $output, $retval);
if ($retval !== 0) {
  // task 2 failed
  exit('Error running task1: ' . implode("<br/>\n", $output));
}

exec('task3.sh', $output, $retval);
if ($retval !== 0) {
  // task 3 failed
  exit('Error running task1: ' . implode("<br/>\n", $output));
}

Upvotes: 1

elkolotfi
elkolotfi

Reputation: 6170

You can simply use the function:

exec ( string $command [, array &$output [, int &$return_var ]] ) : string

With:

output : If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

return_var : If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.

Thus you could use $output and $result_var to check the execution errors of your shells.

Regards

Upvotes: 0

Related Questions