bash - PHP: How to get the error message of failed shell command? -
i execute following command make database backup:
$exec = exec("mysqldump --opt --user=$database_user --password=$database_pass --host=$database_host $database_name > $output_filename", $out, $status);
to check if mysqldump
failed do:
if ($status == 0) { // ok } else { // error // how print error message here ? }
in case goes wrong , mysqldump
fails, how error message ?
you can use proc_open (as suggested emil). below more complete example of how achieve want.
$exec_command = "mysqldump --opt --user=$database_user --password=$database_pass --host=$database_host $database_name" $descriptorspec = array( 0 => array("pipe", "r"), // stdin pipe 1 => array("file", $output_filename, "w"), // stdout file 2 => array("pipe", "w") // stderr pipe ); $proc = proc_open($exec_command, $descriptorspec, $pipes); fwrite($pipes[0], $input); //writing std_in fclose($pipes[0]); $err_string = stream_get_contents($pipes[2]); //reading std_err fclose($pipes[2]); $return_val = proc_close($proc);
edit:
changed output write file
Comments
Post a Comment