尝试在laravel 5.0上使用system()命令php捕获
我有一个在laravel 5.0上用try制作的命令应用程序的代码。。捕获并使用系统命令。当出现异常时,我的捕获不起作用尝试在laravel 5.0上使用system()命令php捕获,php,exception,laravel-5,try-catch,catch-block,Php,Exception,Laravel 5,Try Catch,Catch Block,我有一个在laravel 5.0上用try制作的命令应用程序的代码。。捕获并使用系统命令。当出现异常时,我的捕获不起作用 use Illuminate\Support\Facades\Log; ... try { system('.dump master | sqlite3 ' . $older . ' > ' . storage_path() . '/tmp/' . $tabla . '.sql' ); } catch ( \Exception $e) { Log::alert(
use Illuminate\Support\Facades\Log;
...
try {
system('.dump master | sqlite3 ' . $older . ' > ' . storage_path() . '/tmp/' . $tabla . '.sql' );
} catch ( \Exception $e) {
Log::alert('Problem import old table '. $tabla . $e->GetMessage());
}
我在shell上看到了我的错误,但没有在我自己的laravel日志上写入。(日志:警报)您必须通过
STDERR
以及可能的STDOUT
获得访问权限。使用,例如:
$desc=[
1=>['pipe','w'],//STDOUT
2=>['pipe','w'],//STDERR
];
$proc=proc_open('ls-l.something',$desc,$pipes);
如果(是资源($proc)){
如果($out=stream\u get\u内容($pipes[1])){
回音$out;
}
fclose($pipes[1]);
如果($err=stream\u get\u内容($pipes[2])){
fprintf(标准,“错误:%s\n”,$err);
}
fclose($pipes[2]);
//您还可以检查进程退出状态
//0表示成功,否则表示错误。
$exit_status=proc_close($proc);
}
当然,如果命令将STDOUT重定向到文件,那么就不需要STDOUT管道
是的,system()
不会抛出异常。显然,您可以实现自己的类,如果进程退出状态为非零,或者在STDERR
管道中捕获到某些内容,该类将抛出异常:
类MyShellException扩展\Exception{}
类MyShell{
公共静态函数execute($command,&$out=null){
如果(func_num_args()>1){
$desc[1]=['pipe','w'];
}否则{
$desc[1]=['file','/dev/null'];
}
$desc[2]=[pipe','w'];
$proc=proc_open($command、$desc、$pipes);
如果(是资源($proc)){
if(isset($pipes[1])){
$out=stream\u get\u内容($pipes[1]);
fclose($pipes[1]);
}
如果($err=stream\u get\u内容($pipes[2])){
fclose($pipes[2]);
抛出新的MyShellException(“命令$Command失败:$err”);
}
如果($exit_status=proc_close($proc)){
抛出新的MyShellException(“Command$Command以非零状态退出”);
}
}
}
}
试一试{
MyShell::execute('ls-l.something',$out);
echo“输出:$out\n”;
}捕获(MyShellException$e){
如果(!空($out)){
echo“输出:$out\n”;
}
fprintf(STDERR,“MyShell错误:.$e->getMessage());
出口(1);
}
您必须通过STDERR
以及可能的STDOUT
获得访问权限。使用,例如:
$desc=[
1=>['pipe','w'],//STDOUT
2=>['pipe','w'],//STDERR
];
$proc=proc_open('ls-l.something',$desc,$pipes);
如果(是资源($proc)){
如果($out=stream\u get\u内容($pipes[1])){
回音$out;
}
fclose($pipes[1]);
如果($err=stream\u get\u内容($pipes[2])){
fprintf(标准,“错误:%s\n”,$err);
}
fclose($pipes[2]);
//您还可以检查进程退出状态
//0表示成功,否则表示错误。
$exit_status=proc_close($proc);
}
当然,如果命令将STDOUT重定向到文件,那么就不需要STDOUT管道
是的,system()
不会抛出异常。显然,您可以实现自己的类,如果进程退出状态为非零,或者在STDERR
管道中捕获到某些内容,该类将抛出异常:
类MyShellException扩展\Exception{}
类MyShell{
公共静态函数execute($command,&$out=null){
如果(func_num_args()>1){
$desc[1]=['pipe','w'];
}否则{
$desc[1]=['file','/dev/null'];
}
$desc[2]=[pipe','w'];
$proc=proc_open($command、$desc、$pipes);
如果(是资源($proc)){
if(isset($pipes[1])){
$out=stream\u get\u内容($pipes[1]);
fclose($pipes[1]);
}
如果($err=stream\u get\u内容($pipes[2])){
fclose($pipes[2]);
抛出新的MyShellException(“命令$Command失败:$err”);
}
如果($exit_status=proc_close($proc)){
抛出新的MyShellException(“Command$Command以非零状态退出”);
}
}
}
}
试一试{
MyShell::execute('ls-l.something',$out);
echo“输出:$out\n”;
}捕获(MyShellException$e){
如果(!空($out)){
echo“输出:$out\n”;
}
fprintf(STDERR,“MyShell错误:.$e->getMessage());
出口(1);
}
在php中使用抛出异常。
要使用示例和示例,请执行以下操作:
<?php
/*
*
* opcode number: 108
*/
try {
$error = 'Always throw this error';
throw new Exception($error);
// Code following an exception is not executed.
echo 'Never executed';
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
// Continue execution
echo 'Hello World';
?>
在php中使用抛出异常。 要使用示例和示例,请执行以下操作:
<?php
/*
*
* opcode number: 108
*/
try {
$error = 'Always throw this error';
throw new Exception($error);
// Code following an exception is not executed.
echo 'Never executed';
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
// Continue execution
echo 'Hello World';
?>
外部/系统调用本身不会生成异常。。
?您是希望系统在$PATH
中搜索.dump
可执行文件,还是指执行当前工作目录中的/dump master
?无论如何,我建议构造绝对路径。例如:$cmd=\uuuuuu DIR\uuuuu.'//某处/dump master'
或$cmd=YOUR\u ROOT\u DIR'/bin/dump master'
我对错误不感兴趣。我讨厌尝试…抓住。我知道是什么错误。外部/系统调用本身不会生成异常。。
?您是希望系统在$PATH
中搜索.dump
可执行文件,还是指执行当前工作目录中的/dump master
?无论如何,我建议构造绝对路径。例如:$cmd=\uuuuuu DIR\uuuuu.'//某处/dump master'
或$cmd=YOUR\u ROOT\u DIR'/bin/dump master'
我对错误不感兴趣。我讨厌尝试…抓住。我知道这是什么错误。